七IOC操作Bean管理(Bean生命周期)

Posted 上善若水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了七IOC操作Bean管理(Bean生命周期)相关的知识,希望对你有一定的参考价值。

  1. 生命周期

    • 从对象创建到对象销毁的过程
  2. bean生命周期

    • 通过构造器创建 bean 实例(无参数构造)
    • 为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
    • 调用 bean 的初始化的方法(需要进行配置初始化的方法)
    • bean 可以使用了(对象获取到了)
    • 当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)
  3. 演示bean生命周期

    package com.deewinkg.bean;   
       
    public class Orders {   
        //无参数构造   
        public Orders() {   
            System.out.println("第一步 执行无参数构造创建 bean 实例");   
        }   
        private String oname;   
        public void setOname(String oname) {   
            this.oname = oname;   
            System.out.println("第二步 调用 set 方法设置属性值");   
        }   
        //创建执行的初始化的方法   
        public void initMethod() {   
            System.out.println("第三步 执行初始化的方法");   
        }   
        //创建执行的销毁的方法   
        public void destroyMethod() {   
            System.out.println("第五步 执行销毁的方法");   
        }   
    }  
    
    <?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 id="orders" class="com.deewinkg.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">   
          <property name="oname" value="Spring开发"/>   
       </bean>   
    </beans>  
    
    package com.deewinkg.test;   
       
    import com.deewinkg.bean.Orders;   
    import org.springframework.context.support.ClassPathXmlApplicationContext;   
       
    public class MyTest   
    {   
        @org.junit.Test   
        public void test1()   
        {   
            // ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");   
            ClassPathXmlApplicationContext context =   
                    new ClassPathXmlApplicationContext("bean1.xml");   
            Orders orders = context.getBean("orders", Orders.class);   
            System.out.println("第四步 获取创建 bean 实例对象");   
            System.out.println(orders);   
            //手动让 bean 实例销毁   
            context.close();   
        }   
    }  
    

    运行结果:
    在这里插入图片描述

  4. bean的后置处理器,bean生命周期有七步

    • 通过构造器创建 bean 实例(无参数构造)
    • 为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
    • 把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization
    • 调用 bean 的初始化的方法(需要进行配置初始化的方法)
    • 把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization

    (6)bean 可以使用了(对象获取到了)
    (7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

  5. 演示添加后置处理器效果

    • 创建类,实现接口 BeanPostProcessor,创建后置处理器

      package com.deewinkg.bean;   
         
      import org.springframework.beans.BeansException;   
      import org.springframework.beans.factory.config.BeanPostProcessor;   
         
      public class MyBeanPost implements BeanPostProcessor {   
          @Override   
          public Object postProcessBeforeInitialization(Object bean, String beanName)   
                  throws BeansException {   
              System.out.println("在初始化之前执行的方法");   
              return bean;   
          }   
          @Override   
          public Object postProcessAfterInitialization(Object bean, String beanName)   
                  throws BeansException {   
              System.out.println("在初始化之后执行的方法");   
              return 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 id="orders" class="com.deewinkg.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">   
            <property name="oname" value="Spring开发"/>   
         </bean>   
         <bean id="myBeanPost" class="com.deewinkg.bean.MyBeanPost"></bean>   
      </beans>
      

      运行结果:
      在这里插入图片描述

以上是关于七IOC操作Bean管理(Bean生命周期)的主要内容,如果未能解决你的问题,请参考以下文章

[Spring5]IOC容器_Bean管理_bean的作用域和bean的生命周期

Spring4学习回顾之路06- IOC容器中Bean的生命周期方法

IOC容器中bean的生命周期(简化)

Spring核心技术IoC容器

spring bean的生命周期是怎样的,代码示例

springbean的生命周期是啥?