springbean

Illustration of the instantiation process of Beans in Spring:

 

  1. public class MyBeanPostProcessor implements BeanPostProcessor {  
  2.    private Map<String, ImportListener> importMap = new ConcurrentHashMap<String, ImportListener>();
  3.      public MyBeanPostProcessor() {  
  4.         super();  
  5.         System.out.println( "This is the BeanPostProcessor implementation class constructor!!" );          
  6.      } 
  7.      
  8.      @Override  
  9.      //Instantiation and dependency injection are completed, and some custom initialization tasks are completed before calling the displayed initialization 
  10.      public Object postProcessBeforeInitialization(Object bean, String arg1)  
  11.              throws BeansException {  
  12.          System.out.println( "bean handler: before bean creation.." );  
  13.        
  14.          return bean;  
  15.      }  
  16.      @Override  
  17.     //Instantiate, dependency injection, execute when initialization is complete 
  18.      public Object postProcessAfterInitialization(Object bean, String arg1)  
  19.              throws BeansException {  
  20.          System.out.println( "bean handler: after bean creation.." );  
  21. if (bean instanceof ImportListener) {
  22. ImportListener importListener = (ImportListener) bean;
  23. importMap.put(importListener.getModuleName(), importListener);
  24. }
  25.          return bean;  
  26.      }  
  27.  }  

Notice:

1. Both methods in the interface must return the incoming bean, but cannot return null . If it returns null, then we will not get the target through the getBean method.

 

2. BeanFactory and ApplicationContext treat bean post processors slightly differently. The ApplicationContext will automatically detect all beans that implement the BeanPostProcessor interface in the configuration file, register them as post processors, and then call it when the container creates the bean, so deploying a post processor is the same as deploying other beans and There is no difference. When using the BeanFactory implementation, the bean post-processor must be explicitly registered through the code, and the registration method is defined in the ConfigurableBeanFactory interface in the IoC container inheritance system:

 

在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Singleton模式产生单一实例,对单线程的程序说并不会有什么问题,但对于多线程的程序,就必须注意安全(Thread-safe)的议题,防止多个线程同时存取共享资源所引发的数据不同步问题。
    然而在spring中 可以设定每次从BeanFactory或ApplicationContext指定别名并取得Bean时都产生一个新的实例:例如:
    在spring中,singleton属性默认是true,只有设定为false,则每次指定别名取得的Bean时都会产生一个新的实例
    一个Bean从创建到销毁,如果是用BeanFactory来生成,管理Bean的话,会经历几个执行阶段(如图1.1):
 
    1:Bean的建立:
    容器寻找Bean的定义信息并将其实例化。
    2:属性注入:
    使用依赖注入,Spring按照Bean定义信息配置Bean所有属性
    3:BeanNameAware的setBeanName():
    如果Bean类有实现org.springframework.beans.BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。

    4:BeanFactoryAware的setBeanFactory():
    如果Bean类有实现org.springframework.beans.factory.BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。
    5:BeanPostProcessors的ProcessBeforeInitialization()
    如果有org.springframework.beans.factory.config.BeanPostProcessors和Bean关联,那么其postProcessBeforeInitialization()方法将被将被调用。
    6:initializingBean的afterPropertiesSet():
    如果Bean类已实现org.springframework.beans.factory.InitializingBean接口,则执行他的afterProPertiesSet()方法
    7:Bean定义文件中定义init-method:
    可以在Bean定义文件中使用"init-method"属性设定方法名称例如:
    如果有以上设置的话,则执行到这个阶段,就会执行initBean()方法
    8:BeanPostProcessors的ProcessaAfterInitialization()
    如果有任何的BeanPostProcessors实例与Bean实例关联,则执行BeanPostProcessors实例的ProcessaAfterInitialization()方法
    此时,Bean已经可以被应用系统使用,并且将保留在BeanFactory中知道它不在被使用。有两种方法可以将其从BeanFactory中删除掉(如图1.2):
 
    1:DisposableBean的destroy()
    在容器关闭时,如果Bean类有实现org.springframework.beans.factory.DisposableBean接口,则执行他的destroy()方法
    2:Bean定义文件中定义destroy-method
    在容器关闭时,可以在Bean定义文件中使用"destroy-method"属性设定方法名称,例如:
    如果有以上设定的话,则进行至这个阶段时,就会执行destroy()方法,如果是使用ApplicationContext来生成并管理Bean的话则稍有不同,使用ApplicationContext来生成及管理Bean实例的话,在执行BeanFactoryAware的setBeanFactory()阶段后,若Bean类上有实现org.springframework.context.ApplicationContextAware接口,则执行其setApplicationContext()方法,接着才执行BeanPostProcessors的ProcessBeforeInitialization()及之后的流程。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327067566&siteId=291194637