1 xml的提示配置
1.1 Schema的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring的入门配置 -->
<bean id = "UserDAO" class = "com.heshihua.spring.demo1.UserDAOImpl">
<property name="name" value="李四"></property>
</bean>
</beans>
将xml文件中的http://www.springframework.org/schema/beans/spring-beans.xsd复制下来
打开window->Perferences如下图
将前面复制的地址粘贴到key那一栏
然后点击 File System..选择正确的文件
spring-framework-4.2.4.RELEASE-schema这个文件自行从网上下载
Key Type选择Schema location
具体配置如下图
1.2 Bean的相关配置
1.2.1 bean的id和name配置
id 使用了唯一约束,并且里面不可以使用特殊字符。
name 没有使用唯一约束,理论上可以重复但是在实际开发当中不能重复,但是里面可以出现特殊字符。
以后当把Spring和Struts1框架整合的时候
struts1要求要加上“\”<bean name = "/user" calss = ""/>
很明显id是不能满足的因而只能使用name
还好在Spring中di和name的效果是一样的
1.2.2 bean的生命周期的配置
还是先编写一个接口
package com.heshihua.spring.demo2;
public interface CustomerDAO {
public void save();
}
编写类继承接口
package com.heshihua.spring.demo2;
public class CustomerDAOImpl implements CustomerDAO{
public void setup() {
System.out.println("CustomerDAOImpl被初始化了。。。。。。。");
}
public void save() {
System.out.println("CustomerDAOImpl的save方法执行了。。。。。");
}
public void destroy() {
System.out.println("CustomerDAOImpl被销毁了。。。。。。");
}
}
编写bean
<!-- Spring的bean的生命周期的配置 -->
<bean id = "customerDAO" class = "com.heshihua.spring.demo2.CustomerDAOImpl" init-method = "setup" destroy-method = "destroy"></bean>
编写测试类
package com.heshihua.spring.demo2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDome2 {
/*
* 生命周期的配置
* **/
@Test
public void demo1() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDAO customerDAO = (CustomerDAO) applicationContext.getBean("customerDAO");
customerDAO.save();
applicationContext.close();
}
}
运行结果如下图
init-method在你在创建customerDAO对象的时候,就会自动调用其所对应的setup方法
destroy-method在你在销毁customerDAO对象的时候,就会自动调用其所对应的destroy方法(当bean是单利模式的时候);
1.2.3 bean的作用范围的配置(重点)
scope 配置bean的作用范围
singleton :默认的,Springhi使用单例模式创建这个对象
prototype :多例模式(在整合struts2的时候会用到)
request :应用在web项目中,Spring创建这个类会将这个类存入到request的范围中
session :应用在web项目中,Spring创建这个类会将这个类存入到session的范围中
globalsession :应用在web项目中,必须在porlet环境中使用。如果没有这中环境就相当于session的作用
测试默认是否是单例模式、
@Test
public void demo2() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDAO customerDAO1 = (CustomerDAO) applicationContext.getBean("customerDAO");
CustomerDAO customerDAO2 = (CustomerDAO) applicationContext.getBean("customerDAO");
System.out.println(customerDAO1);
System.out.println(customerDAO2);
System.out.println(customerDAO1==customerDAO2);
}
运行结果如下图
结果证明他只实例化了一次
俩个对象实际上是一个对象,地址也是一样的
set-up函数只运行了一次
所以说默认是单例模式
如果把scope配置成singleton
结果是一样的这里不上结果图了没意义(爱信不信哈哈)
如果把scope配置成prototype
如图所示变成多例模式了
在这里如果调用applicationContext.close()会发现不会销毁成功。因为存在多个对象他不知道取销毁哪一个。