Spring-Bean的生命周期

自动装配的含义是,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>

然后输出结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值