1、Spring常用的两种注入方式
(1)设值注入
通过set方法注入,bean里面的property属性的name值需要与类中的属性名字一样
-
<beans>
-
<bean id="studentImpl" class="cn.test.interImpl.StudentImpl"></bean>
-
<bean id="test" class="cn.test.action.Test">
-
<property name="iStudent" ref="studentImpl"></property>
-
</bean>
-
</beans>
-
public class Test {
-
private IStudent iStudent;
-
public void setiStudent(IStudent iStudent) {
-
this.iStudent = iStudent;
-
}
-
public void printInfo(String name, int age) {
-
System.out.println(iStudent.getStudent(name, age));
-
}
-
}
(2)构造注入
-
<beans>
-
<bean id="studentImpl" class="cn.test.interImpl.StudentImpl"></bean>
-
<bean id="test" class="cn.test.action.Test">
-
<constructor-arg ref="studentImpl"></constructor-arg>
-
</bean>
-
</beans>
-
public class Test {
-
private IStudent iStudent;
-
public Test(IStudent iStudent) {
-
super();
-
this.iStudent = iStudent;
-
}
-
public void printInfo(String name, int age) {
-
System.out.println(iStudent.getStudent(name, age));
-
}
-
}
2、Bean的配置项及作用域
(1)Bean的常用配置项:只有class是必须配置的,其他项可以不用配置
a、id 整个IOC容器内的唯一标识
b、class 具体要实例化的类
c、scope 作用范围
d、constructor arguments 构造器参数
e、properties 属性(设值注入、构造方法注入)
f、autowriting mode 自动装配的模式
g、lazy-initialization mode 懒加载模式
h、initialization/destruction method 初始化、销毁方法
(2)Bean的作用域scope
a、singleton 单例,指在一个Spring IOC容器中只存在一个
b、prototype 每次请求创建新的实例,destory方式不生效
c、request 每次http请求创建一个实例,且只在当前的request内有效
d、session 每次http请求创建,且只在当前session内有效
e、global session 基于portlet的web中有效(portlet定义了global session),如果在web中,同session
3、Bean的生命周期
(1)定义
在配置文件中对bean的定义包括id、class、property等属性的配置
(2)初始化
IOC容器启动的时候去加载配置文件里面的bean初始化,也就是生成bean的实例
a、实现InitializingBean接口,覆盖afterPropertiesSet方法
-
public class StudentImpl implements IStudent, InitializingBean {
-
@Override
-
public String getStudent(String name, int age) {
-
return "我的名字叫" + name + ", 我今年" + age + "岁~";
-
}
-
@Override
-
public void afterPropertiesSet() throws Exception {
-
System.out.println("studentImpl初始化");
-
}
-
}
b、在bean的属性中配置init-method
<bean id="studentImpl" class="cn.test.interImpl.StudentImpl" init-method="init"></bean>
-
public class StudentImpl implements IStudent {
-
@Override
-
public String getStudent(String name, int age) {
-
return "我的名字叫" + name + ", 我今年" + age + "岁~";
-
}
-
public void init() {
-
System.out.println("init: studentImpl初始化");
-
}
-
}
c、配置全局默认的初始化方法default-init-method
-
<beans default-init-method="init" default-destroy-method="destory">
-
<bean id="studentImpl" class="cn.test.interImpl.StudentImpl"></bean>
-
<bean id="test" class="cn.test.action.Test">
-
<constructor-arg ref="studentImpl"></constructor-arg>
-
</bean>
-
</beans>
说明:当3种方法都使用时,会先执行覆盖的接口方法afterPropertiesSet,再执行bean自定义的init-method中的方法,全局默认的初始化default-init-method中的方法不执行。bean的全局默认初始化方法在配置文件中配置后,bean可以不实现该方法,但另外两种必须实现。
(3)使用
在实际开发过程中从IOC容器里面取出一个bean的实例,调用实例中的方法
(4)销毁
在IOC容器停止的时候去销毁由当前容器创建的所有bean的实例
a、实现DisposableBean接口,覆盖destroy方法
-
public class StudentImpl implements IStudent, DisposableBean {
-
@Override
-
public String getStudent(String name, int age) {
-
return "我的名字叫" + name + ", 我今年" + age + "岁~";
-
}
-
@Override
-
public void destroy() throws Exception {
-
System.out.println("destroy: studentImpl销毁");
-
}
-
}
b、在bean的属性中配置destroy-method
<bean id="studentImpl" class="cn.test.interImpl.StudentImpl" destroy-method="des"></bean>
-
public class StudentImpl implements IStudent {
-
@Override
-
public String getStudent(String name, int age) {
-
return "我的名字叫" + name + ", 我今年" + age + "岁~";
-
}
-
public void des() {
-
System.out.println("destory: studentImpl销毁");
-
}
-
}
c、配置全局默认的销毁方法default-destroy-method
-
<beans default-init-method="init" default-destroy-method="destory">
-
<bean id="studentImpl" class="cn.test.interImpl.StudentImpl"></bean>
-
<bean id="test" class="cn.test.action.Test">
-
<constructor-arg ref="studentImpl"></constructor-arg>
-
</bean>
-
</beans>
说明:当3种方法都使用时,会先执行覆盖的接口方法destroy,再执行bean自定义的destroy-method中的方法,全局默认的初始化default-destroy-method中的方法不执行。bean的全局默认销毁方法在配置文件中配置后,bean可以不实现该方法,但另外两种必须实现。
4、Aware接口
Spring中提供了一些以Aware结尾的接口,实现了Aware接口的Bean在在被初始化之后,可以获取相应的资源。通过Aware接口,可以对Spring相应的资源进行操作(一定要慎重)。Aware为对Spring进行简单的扩展提供了方便的入口。
(1)实现BeanNameAware
-
public class MyBeanName implements BeanNameAware {
-
@Override
-
public void setBeanName(String name) {
-
System.out.println("beanName: " + name);
-
}
-
}
(2)实现ApplicationContextAware
-
public class MyApplicationContext implements ApplicationContextAware {
-
@Override
-
public void setApplicationContext(ApplicationContext context)
-
throws BeansException {
-
System.out.println(context.getBean("myApplicationContext").hashCode());
-
}
-
}
说明:可以在同一个类中同时实现这两个Aware接口,然后使用context.getBean("beanName")获取IOC容器中的bean实例。
5、Bean的自动装配
(1)No:不做任何操作
(2)byName:根据属性名自动装配。此选项将检查容器并根据名字查找完全一致的bean,并将其与属性自动装配
名称必须一致
-
<beans default-autowire="byName">
-
<bean id="autoWireDAO" class="cn.test.autowire.AutoWireDAO"></bean>
-
<!-- AutoWireService中有属性autoWireDAO,并添加了该属性的set方法 -->
-
<bean id="autoWireService" class="cn.test.autowire.AutoWireService"></bean>
-
</beans>
(3)byType:如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配;如果存在多个该类型的bean,那么抛出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的bean,则什么事都不会发生
类型必须一致,名称可以不一致
-
<beans default-autowire="byType">
-
<bean id="autoWireDAO" class="cn.test.autowire.AutoWireDAO"></bean>
-
<!-- AutoWireService中有属性autoWireDAO,并添加了该属性的set方法 -->
-
<bean id="autoWireService" class="cn.test.autowire.AutoWireService"></bean>
-
</beans>
(4)constructor:与byType方式类似,不同之处在于它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常
类型必须一致,名称可以不一致
-
<beans default-autowire="constructor">
-
<bean id="autoWireDAO" class="cn.test.autowire.AutoWireDAO"></bean>
-
<!-- AutoWireService中有属性autoWireDAO,并添加了以该属性为参数的构造方法 -->
-
<bean id="autoWireService" class="cn.test.autowire.AutoWireService"></bean>
-
</beans>