Bean Spring5 source code analysis of the life cycle

The life cycle

  Creating ----> initialize ---> Destruction

1. Examples of objects

2. setter injection execution Bean attribute dependency injection

3. BeanNameAware the setBeanName (), if the interface is achieved, which is executed method setBeanName

4.BeanFactoryAware the setBeanFactory (), if the interface is achieved, which is executed method setBeanFactory

5. BeanPostProcessor the processBeforeInitialization (), if the associated processor, it will be executed before this example processBeforeInitialization Bean initialization () method

6. InitializingBean the afterPropertiesSet (), if the interface is achieved, which is executed afterPropertiesSet () for. Bean definition file defined in init-method

7.BeanPostProcessors of processAfterInitialization (), if there is an associated processor, it will perform this instance processAfterInitialization before Bean initialization () method

8.DisposeablebBean of destory (), when the container is closed, if the Bean implements this interface, perform his destory () method

9. The definition file defines Bean destroy-method, when the container is closed, may be used "destory-method" defined in the definition file Method Bean

 

Note:

 1. Single cases of default in the container is initialized when love Kerry

 2. In the cases of each of the object when it will get Bean to initialize

 


 

 

 bean initialization refers to the object has been created inside all set up methods have been implemented. The method of execution specified

   @Bean (initMethod, Destory) specifies the initialization and destruction methods at the same time to create these two methods in a bean

   init is executed before or after the constructor? Constructor with no arguments! Objects created after the first initialized! So the first implementation of no-argument constructor!

 

Supplementary to the above process:

  Bean created (the constructor is executed) -> Initialize (custom init method) -> destruction

  Call the close () method to destroy singleton object  

   Note: IOC container storage in conjunction with the use Map objects, clear () Clear Object

   Look Source:

Enter Views:

Click continue to view:

First look:

clear method after continuous follow-up is a collection of the

 

 We can go through the initialization operation to achieve certain class!

 

Develop ways to use:


Method a: Specifies init-method and destory-method by @Bean

Method two: by letting Bean implementation InitializingBean (defined initialization logic), DisposableBean (defined destroy logic)

Method three: Use the JSR250 (Java specification, not the Spring): @PostConstructo: created at Bean and the assignment is completed, to perform initialization method. @PreDestory: let us work to clean up before the container destroyed Bean


 

Method Two:

Bean:

@Component
public class LifeBean implements InitializingBean, DisposableBean {

    //构造函数
    public LifeBean() {
        System.out.println("LifeBean Constructor");
    }

    /** 接口InitializingBean的方法
     *  //解释 对象有创建 肯定也有给属相赋值的过程!,对象赋值完毕以后才执行该方法  即: (afterPropertiesSet)中文:set方法都走完了时候执行该方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //等同于 @Bean(init =" ")
        //解释 对象有创建 肯定也有给属相赋值的过程!,对象赋值完毕以后才执行该方法
        System.out.println("LifeBean ********> 【InitializingBean.afterPropertiesSet】 ");
    }

    /**
     * 接口DisposableBean 的方法
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("LifeBean ********>【DisposableBean.destroy】");
    }
}

配置和扫包

@Configuration
@ComponentScan("com.toov5.config.beanTest.entity")
public class MyConfig {

}

启动测试:

public class test {
    public test(){

    }
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext1 = new AnnotationConfigApplicationContext("com.toov5.config");
        applicationContext1.close();
//        System.out.println(applicationContext1);
    }
}

 

方法三

  注解代替了接口:

Bean:

@Component
public class LifeBean {

    //构造函数
    public LifeBean() {
        System.out.println("LifeBean Constructor");
    }
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        //等同于 @Bean(init =" ")
        //解释 对象有创建 肯定也有给属相赋值的过程!,对象赋值完毕以后才执行该方法
        System.out.println("LifeBean ********> 【InitializingBean.afterPropertiesSet】 ");
    }

   @PreDestroy
    public void destroy() throws Exception {
        System.out.println("LifeBean ********>【DisposableBean.destroy】");
    }
}

效果是一样的:

 

  

 Spring Bean生命周期中的后置处理器:  BeanPostProcessor接口


过滤器中,不可以使用注解方式获取Bean对象。 做法: 单独获取上下文ApplicationContext

后置处理器,实现对Bean初始化增强功能

@Component
public class MyApplicationContext implements ApplicationContextAware {
    //开发时候经常做成全局的 进行使用
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LifeBean lifeBean = applicationContext.getBean("lifeBean", LifeBean.class);
        System.out.println("result————————————————————"+lifeBean.toString());
    }
}

运行后:结果是没问题的

 

Guess you like

Origin www.cnblogs.com/toov5/p/11257106.html