Spring annotation-driven development (six) ----- spring container creates [Source]

refresh Spring container () [create] Refresh

1, prepareRefresh () refresh pretreatment

1), initPropertySources () to initialize some property; property personalized approach subclass Custom;
2), getEnvironment () validateRequiredProperties ();. Legal inspection property, etc.
3), earlyApplicationEvents = new LinkedHashSet < ApplicationEvent> () ; save some of the early events in the container;

2, obtainFreshBeanFactory (); obtaining the BeanFactory;

1), refreshBeanFactory (); refresh [create] BeanFactory;
create a this.beanFactory = new DefaultListableBeanFactory (); setting the above mentioned id;
2), getBeanFactory (); return BeanFactory object is created just GenericApplicationContext;
3), created BeanFactory [] DefaultListableBeanFactory return;

3, prepareBeanFactory (beanFactory); BeanFactory pre-preparation (the BeanFactory some settings);

1), the class loader is provided BeanFactory support expression parser ...
2), adding section BeanPostProcessor] [ApplicationContextAwareProcessor
3), the interface provided EnvironmentAware automatic assembly negligible, EmbeddedValueResolverAware, XXX;
. 4), the register can be resolved automated assembly; we can directly inject automatically in any component: BeanFactory, ResourceLoader, ApplicationEventPublisher, ApplicationContext
5), add BeanPostProcessor] [ApplicationListenerDetector
6), add AspectJ compile-time;
7), to register the BeanFactory some components usable;
ConfigurableEnvironment [environment], systemProperties [Map <String, Object>], systemEnvironment [Map <String, Object>]

4、postProcessBeanFactory(beanFactory)

Post-processing performed after the BeanFactory preparations are completed;

1), by subclasses override this method to create the BeanFactory and pre-prepared after the completion of further settings
====================== above is to create a BeanFactory and pre-preparation ==================================

5, invokeBeanFactoryPostProcessors (beanFactory) ----- method of performing a BeanFactoryPostProcessor

BeanFactoryPostProcessor: BeanFactory postprocessor. BeanFactory performed after the standard initialization;
two interfaces: BeanFactoryPostProcessor, BeanDefinitionRegistryPostProcessor

1) A method of performing BeanFactoryPostProcessor;    

First implementation BeanDefinitionRegistryPostProcessor
    . 1), acquires all BeanDefinitionRegistryPostProcessor;
    2), see achieved BeanDefinitionRegistryPostProcessor PriorityOrdered performed first priority of the interface,
     postProcessor.postProcessBeanDefinitionRegistry (Registry)
    . 3), performed in order to achieve a BeanDefinitionRegistryPostProcessor Ordered interface;
        postProcessor.postProcessBeanDefinitionRegistry (Registry )
    4), and finally perform not implement any order of priority or BeanDefinitionRegistryPostProcessors interface;
     postProcessor.postProcessBeanDefinitionRegistry (Registry)

2), then perform a method BeanFactoryPostProcessor

  1), obtaining all the BeanFactoryPostProcessor
  2), see achieved BeanFactoryPostProcessor PriorityOrdered performed first priority of the interface,
      postProcessor.postProcessBeanFactory ()
  . 3), performed in order to achieve a BeanFactoryPostProcessor Ordered interface;
      postProcessor.postProcessBeanFactory ()
  . 4), finally performed does not implement any order of priority or BeanFactoryPostProcessor interface;
      postProcessor.postProcessBeanFactory ()

6, registerBeanPostProcessors (beanFactory) ----- registration BeanPostProcessor (Bean post-processors) [intercept bean creation]

Different interface types BeanPostProcessor; create the opportunity to perform before and after the Bean is not the same
BeanPostProcessor, DestructionAwareBeanPostProcessor, InstantiationAwareBeanPostProcessor, SmartInstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor [internalPostProcessors],
1), acquired all of BeanPostProcessor; postprocessor by default through PriorityOrdered, Ordered Interface performing priority
2), the first priority register PriorityOrdered BeanPostProcessor interface; to each BeanPostProcessor; added to the BeanFactory beanFactory.addBeanPostProcessor (PostProcessor);
. 3), then Ordered registration interface
4), and finally achieved without any priority register interface
5), final registration MergedBeanDefinitionPostProcessor;
6), registering a ApplicationListenerDetector; to create a complete bean after checking whether ApplicationListener, if it is applicationContext.addApplicationListener ((ApplicationListener) bean) < ?>;

7, initMessageSource (); initialization MessageSource assembly (do internationalization features; binding message, message parsing)

1) to obtain BeanFactory
2), if there is messageSource see id container, the type is MessageSource components; if there are assigned to messageSource, if not their own to create a DelegatingMessageSource;

MessageSource: takes a value of a key international profile; can be acquired in accordance with the area information;
3), to create a good MessageSource registered in the container after obtaining the value of international profile, they can automatically inject MessageSource;

beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);

MessageSource.getMessage(String code, Object[] args, String defaultMessage, Locale locale);

8, initApplicationEventMulticaster (); initialize event dispatcher;

1), obtaining BeanFactory
2), acquired from the BeanFactory applicationEventMulticaster of ApplicationEventMulticaster;
3), if the previous step is not configured; create a SimpleApplicationEventMulticaster
. 4), created ApplicationEventMulticaster added to the BeanFactory, directly after the other components of the automatic injector

9, onRefresh (); left subtank (subclass)

1, a subclass overrides this method, when the container refresh logic can be customized;

10, registerListeners (); for all items in the inside of the container ApplicationListener registered in;

1, from the container to get all the ApplicationListener
2, adding to each listener in an event dispatcher;
. GetApplicationEventMulticaster () addApplicationListenerBean (listenerBeanName);
. 3, before the step of generating event distribution;

11, finishBeanFactoryInitialization (beanFactory); initialize all remaining single instance of the bean;

1, beanFactory.preInstantiateSingletons (); remaining after initialization single instance the bean
  . 1), acquires all Bean container sequentially to initialize and create objects
  2), acquires the definition information Bean; RootBeanDefinition
  . 3), not abstract Bean, is a single instance, not lazy loading;
    1), determines whether FactoryBean; if the interface is implemented FactoryBean Bean;
    2), is not a plant Bean. Using getBean (beanName); Create Object
        0, getBean (beanName); ioc.getBean ();
        . 1, doGetBean (name, null, null, to false);
        2, stored in the cache to obtain a single instance Bean. If you can get to explain is created through (all created over a single instance of Bean will be cached) before the Bean
        from private final Map <String, Object> singletonObjects = new ConcurrentHashMap <String, Object> (256); acquisition of
        3, get less than the cache, the process began to create objects of bean;
        4, has been created to mark the current bean
        5, acquire the definition information bean;
        6, [for additional bean bean currently dependent; if there is follow getBean () the dependent bean create it;]
        7, start a single instance of Bean creation process;
        1), createBean (beanName, MBD, args);
        2), Object bean = resolveBeforeInstantiation (beanName, mbdToUse); let BeanPostProcessor first interception return proxies;
          [InstantiationAwareBeanPostProcessor]: Execute in advance;
            first trigger: postProcessBeforeInstantiation ();
            if there is a return value: trigger postProcessAfterInitialization ();
        3), if the previous InstantiationAwareBeanPostProcessor not return proxies; call 4)
        4), Object beanInstance = doCreateBean (beanName, mbdToUse, args); create Bean
            1 ), [] Bean instance created; createBeanInstance (beanName, mbd, args );
               constructor or by using a factory method to create the object Bean instance;
            2), applyMergedBeanDefinitionPostProcessors (MBD, BeanType, the beanName);
             The call MergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition (MBD, BeanType, the beanName);
            . 3), [] attribute assignment Bean populateBean (beanName, mbd, instanceWrapper) ;
              prior to the assignment:
              1), to get the post-processor InstantiationAwareBeanPostProcessor;
                  postProcessAfterInstantiation ();
              2), InstantiationAwareBeanPostProcessor get the post-processor;
                  postProcessPropertyValues ();
                  before ===== assignment: ===
              3), the value of the application attribute Bean; assign attribute using the setter method;
                  applyPropertyValues (the beanName, MBD, BW, PVS );
            4), [] initialized Bean initializeBean (beanName, exposedObject, mbd) ;
              1), [] interface method performed Aware invokeAwareMethods (beanName, bean); xxxAware interface method performed
                 BeanNameAware \ BeanClassLoaderAware \ the BeanFactoryAware
              2), before performing the post-processor [] initialized applyBeanPostProcessorsBeforeInitialization (wrappedBean, the beanName);
                 BeanPostProcessor.postProcessBeforeInitialization () ;
              3), [perform initialization method] invokeInitMethods (the beanName, wrappedBean, MBD);
                 . 1), whether it is implemented InitializingBean interface; initialization execution interfaces defined;
                 2), whether custom initialization method;
              4), [after performing opposite after the processor initializes] applyBeanPostProcessorsAfterInitialization
                 BeanPostProcessor.postProcessAfterInitialization ();
            5), methods of destruction of registration Bean;
        5), would create the Bean added to the cache singletonObjects;
            ioc container is the Map; Map which holds a lot of single-instance Bean, environmental information. . . . ;
            All take advantage of Bean after getBean created;
            Inspect all Bean is SmartInitializingSingleton interface; if it is; on the implementation of afterSingletonsInstantiated ();

12, finishRefresh (); BeanFactory completed initialization to create work; IOC container to create complete;

1), initLifecycleProcessor (); post processor initialization and life-cycle-related; LifecycleProcessor
default to find whether there lifecycleProcessor components] [LifecycleProcessor from the container; if there is no new DefaultLifecycleProcessor ();
added to the vessel;
write a LifecycleProcessor implementation class , may be the BeanFactory
void OnRefresh ();
void the onClose ();
2), getLifecycleProcessor () OnRefresh ();.
got previously defined lifecycle processor (the BeanFactory); callback OnRefresh ();
. 3), the publishEvent (new new ContextRefreshedEvent (this)); publishing refresh is complete container events;
4), liveBeansView.registerApplicationContext (the this);

====== =========== summary

1), Spring containers at startup, the first will save the definition information Bean all registered incoming;
    1), xml registration bean; <bean>
    2), notes registered Bean; @ Service, @ the Component, @ Bean, xxx
2 ), Spring containers will be the right time to create these bean
    1), when used in the bean; getBean created using the bean; later created stored in a container;
    2), unified all the rest of creation when the bean; finishBeanFactoryInitialization () ;
    3), the post processor; the BeanPostProcessor
     . 1), each bean is created, the processor will use a variety of post-processing; bean enhanced features;
         the AutowiredAnnotationBeanPostProcessor: automatic injection process
         AnnotationAwareAspectJAutoProxyCreator: do AOP function;
         XXX. ...
        enhanced features notes:
           AsyncAnnotationBeanPostProcessor
        ....
4), event-driven model;
    ApplicationListener; event listener;
    ApplicationEventMulticaster; event-dispatching:

Guess you like

Origin www.cnblogs.com/alimayun/p/11123836.html