Spring 学习笔记心得(六)IOC容器生命周期详解

IOC容器中Bean的生命周期方法

Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务.

  1. Spring lOC容器对Bean的生命周期进行管理的过程:
  2. 通过构造器或工厂方法创建Bean实例为Bean的属性设置值和对其他Bean的引用
    C
    调用Bean的初始化方法
    Bean可以使用了
    -当容器关闭时,调用Bean的销毁方法
  3. 在Bean的声明里设置init-method和destroy-method属
    性,为Bean指定初始化和销毁方法.

1.创建接口类

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      
        return bean;
    }

1.2 创建User_Post类(用来测试生命周期)

public class User_Post {

    private String name;
    private String sex;

    public User_Post() {
        System.out.println("User_Post Constractor's ... ");
    }


    public void setName(String name) {
        System.out.println("setting ... ");
        this.name = name;
    }

    public void init(){
        System.out.println("init....");
    }

    public void destory(){
        System.out.println("destroy....");
    }
       .....
 }

1.3 配置后置管理器

<!-- 配置后置管理器-->
<bean class="cn.itcast.spring.beans.PostProcessor.MyBeanPostProcessor"/>

1.4 创建User_Post Bean类


<bean id="user" class="cn.itcast.spring.beans.domain.User_Post"
      init-method="init"
      destroy-method="destory">
    <property name="name" value="ZhangS" />
    <property name="sex" value="Male" />
</bean>

注意:这里要bean标签中的两个参数,init-method=“init”(这里是指向User_Post中的init方法,方法名要对应),destroy-method=“destory”(这里是指向User_Post中的destory方法,方法名要对应)

1.5 创建测试类

 ApplicationContext context = new ClassPathXmlApplicationContext("bean-post.xml");
        User_Post user_post = (User_Post) context.getBean("user");
        System.out.println(user_post);

1.6 输出结果:

顺序依次是:

1.6.1 构造器最先被创建:输出

User_Post Constractor’s …

1.6.2 set方法被调用:输出

setting …

1.6.3 接口的postProcessBeforeInitialization 方法被调用:输出

postProcessBeforeInitialization Method …

1.6.4 init方法:输出

init…

1.6.5 接口的postProcessAfterInitialization方法:输出

postProcessAfterInitialization Method …

1.6.6 User_Post中的destory方法:输出

destroy…

所有顺序为:
User_Post Constractor’s … (构造器方法)
setting … (设置方法)
postProcessBeforeInitialization Method … (BeforeInitialization方法)
init… (init方法)
postProcessAfterInitialization Method … (AfterInitialization方法)
destory … (最后的摧毁方法)

1.7 关于接口中的两个实现方法的过滤使用(postProcessBeforeInitialization ,postProcessAfterInitialization )

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        System.out.println("bean :  " + bean + " : " +   " beanName : " + beanName);
        System.out.println("postProcessAfterInitialization Method ... ");
        //可以判断ID是否是你想指定的
        if (“id”(实体类ID).equals(beanName)){
         过滤一系列操作
            .....
            
        }
        return bean;
    }

好了 , 这里就是关于IOC容器生命周期的相关内容了,如果有讲的不对的地方还请大家指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值