Bean的生命周期

目录

1、生命周期

2、bean的生命周期


1、生命周期

就是从对象创建到对象销毁的过程

2、bean的生命周期

(1)通过构造器创建bean的实例(无参数构造)

(2)为bean的属性设置值和对其他bean引用(调用set方法)

         (2.5)把bean实例传递给bean的后置处理器方法

(3)调用bean的初始化方法(需进行配置初始化的方法)

         (3.5)把bean实例传递给bean的后置处理器方法

(4)bean可以进行使用

(5)当容器关闭的时候,调用bean的销毁方法(需进行配置销毁的方法)

进行代码测试

建立Test类

public class Test {
    private String name;
    private String age;

    public Test() {
        System.out.println("1、调用无参构造");
    }
    public void setAge(String age) {
        System.out.println("2、调用set方法设置属性值");
        this.age = age;
    }
    //创建初始化方法
    public void initMethod(){
        System.out.println("3、初始化方法");
    }
    //创建初始化方法
    public void destroyMethod(){
        System.out.println("5、销毁方法");
    }

}

配置XMl

init-method="initMethod"的意思是初始化时执行initMethod方法
destroy-method="destroyMethod"的意思是销毁时时执行destroyMethod方法 

注意不要再在prototype模式下执行,不然不会执行destroyMethod方法

<?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">
    <bean name="test" class="com.zth.Test" init-method="initMethod" destroy-method="destroyMethod">
        <property name="age">
            <value>"22"</value>
        </property>
    </bean>
</beans>

主方法执行

public class Main {
    public static void main(String[] args) {
        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/zth/xml/testbean.xml") {
        };
        Test t1 = applicationContext.getBean("test",Test.class);
        System.out.println("4、获取到bean的实例对象");
        //手动销毁
        ((ClassPathXmlApplicationContext)applicationContext).close();

    }
}

运行结果

 

bean此后还会有后置方法 ,加上后会有7步

首先实现

处理器类

public class MyPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("2.5、初始化前——后置处理器执行");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("3.5、初始化后——后置处理器执行");
        return bean;
    }
}

在XMl文件为所用bean添加后置处理器,这里要设置为多例模式

<?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">
    <bean name="test" class="com.zth.Test" scope="prototype" init-method="initMethod" destroy-method="destroyMethod">
        <property name="age">
            <value>"22"</value>
        </property>
    </bean>
            <!--   为所有bean添加处理器     -->
    <bean id="myPost" class="com.zth.MyPost"></bean>
</beans>

运行

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值