自动装配的含义是,javaBean之间的依赖关系,用Autowire 代表的是byName类型,进行装配。而依赖注入是指对这个bean里面的属性赋值,以是一个Bean的生命周期。
1、实例化bean对象(通过构造方法或者工厂方法)
2、设置对象属性(setter等)(依赖注入)
3、如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。(和下面的一条均属于检Aware接口)
4、如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身
5、将Bean实例传递给Bean的前置处理器的postProcessBeforeInitialization(Object bean, String beanname)方法
6、调用Bean的初始化方法,初始化方法可以自定义。
7、将Bean实例传递给Bean的后置处理器的postProcessAfterInitialization(Object bean, String beanname)方法
8、使用Bean
9、容器关闭之前,调用Bean的销毁方法
为了方便理解,通过一个小例子来实现这个生命周期。
public class Student implements BeanNameAware{
private String name;
public Student(){
super();
}
public void setName(String name) {
System.out.println("设置对象属性setName");
this.name = name;
}
public void initMethod(){
System.out.println("初始化student这个bean");
}
public void destroyMethod(){
System.out.println("销毁方法student这个bean");
}
public void playMethod(){
System.out.println("使用了student这个bean ");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
/**
* 重写setBeanName方法
* @param s
*/
@Override
public void setBeanName(String s) {
System.out.println("调用了 BeanNameWare接口中重写setBeanName方法");
}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* 初始化之前对Bean进行处理
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("对初始化之前的Bean进行处理,此时我的名字"+bean);
return bean;
}
/**
* 初始化之后对Bean处理
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Student stu=null;
if("name".equalsIgnoreCase(beanName) || bean instanceof Student){
stu=(Student)bean;
stu.setName("njl");
}
System.out.println("初始化之后对Bean处理,修改了成员变量的值"+bean);
return stu;
}
}
测试类
public class BeanCycleTest {
@Test
public void beanCycleTest(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext1.xml");
Student st=(Student) ac.getBean("student");
st.playMethod();
System.out.println(st);
((AbstractApplicationContext)ac).close();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="student" class="com.jd.model.Student" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="jack"/>
</bean>
//此处 不用写id, spring 会自动识别实现BeanPostProcessor这个接口的类
<bean class="com.jd.model.MyBeanPostProcessor"/>
</beans>
然后输出结果