Research BeanFactory core container: spring source code analysis Series 3

table of Contents

@ (spring Source Analysis Series: Study of the core container)
before speaking container, then clear about knowledge.

  • BeanDefinition Bean is described in the container. BeanDefinition and Bean is not a thing.
  • Bean is created out according to BeanDefinition. That is what we call the object.

BeanDefinition need a place to store materials, Bean needs a place to store finished products. Today we talk about the warehouse.

BeanFactory family

Here Insert Picture Description
This figure is the default container inherited DefaultListableBeanFactory achieve diagram. We next analyzed from right to left.

  1. BeanFactory interface:
    a container top-level interface, basic capability provides a container, comprising obtaining bean, bean is included, whether a single embodiment, acquiring the type of bean, Bean alias like.
  2. ListableBeanFactory Interface:
    sub-interface of BeanFactory; has the ability to get the bulk of Bean
  3. HierarchicalBeanFactory interfaces: the ability to access the parent container. There are levels of BeanFactory.
  4. AutowireCapableBeanFactory Interface: Inheriting BeanFactory, extends the automatic assembly capacity. This interface is used for a greater role for integration with other frameworks, the spring is not added to the container Bean Spring container life cycle management in the past. This interface is rarely used
  5. ConfigurableBeanFactory: defines the BeanFactory configuration. HierarchicalBeanFactory inheritance and SingletonBeanRegistry interface. Container implements this interface, hierarchical, singleton BeanDefinition registration function.
  6. ConfigurableListableBeanFactory:
    Large fusion interface, can have in addition to the above interfaces, further comprising: a class loader, conversion type, property editor, the BeanPostProcessor, scope, defined bean, bean processing dependencies, bean destruction like.

  7. SingletonBeanRegistry Interface: Bean has the ability to operate registration, query, access number, etc. Bean Bean ability to pay attention to here is different from the examples BeanDefinition...

  8. SimpleAliasRegistry:. Bean alias class operation, has achieved AliasRegistry storage Bean alias, alias registered Bean, Bean alias obtain functional properties are stored Bean alias .aliasMap

  9. DefaultSingletonBeanRegistry:. SimpleAliasRegistry addition to inheriting the functions of the most important is to achieve the ability to interface with storage SingletonBeanRegistry Bean instance, registered Bean, Bean get between the instance of an object class Class Spring management of our definitions, and examples. the interdependence of these are stored in the default-related capacity Bean common container of DefaultListableBeanFactory. such is through indirect inheritance to achieve.
    /** Disposable bean instances: bean name --> disposable instance */
    private final Map<String, Object> disposableBeans = new LinkedHashMap<String, Object>();

    /** Map between containing bean names: bean name --> Set of bean names that the bean contains */
    private final Map<String, Set<String>> containedBeanMap = new ConcurrentHashMap<String, Set<String>>(16);

    /** Map between dependent bean names: bean name --> Set of dependent bean names */
    private final Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<String, Set<String>>(64);

    /** Map between depending bean names: bean name --> Set of bean names for the bean's dependencies */
    private final Map<String, Set<String>> dependenciesForBeanMap = new ConcurrentHashMap<String, Set<String>>(64)
  1. FactoryBeanRegistrySupport: Bean provides multi-plant support .FactoryBean by its name I can see is the production of Bean of Bean.
  2. AbstractBeanFactory abstract class: the nexus from the figure we see AbstractBeanFactory by inheritance, succession FactoryBeanRegistrySupport capabilities and to achieve the right part of the interface is already relatively complete the .AbstractBeanFactory Bean container defines Get Bean template mode..... the algorithm skeleton,
  3. AbstractAutowireCapableBeanFactory:. Most have the ability to implement a template method AbstractBeanFactory defined method which doCreateBean logic is to become a BeanDefinition Bean process... This method is very important we usually use the class to create new objects directly out of the BeanDefinition spring... Bean to process templated, leaving a lot of extension points. BeanDefition left to the user to customize the process of creating the Bean at different times.
  4. DefaultListableBeanFactory achieve common default container, the container class is also the most commonly used spring. Look left DefaultListableBeanFactory achieved BeanDefinitionRegistry interface. What does this mean? BeanDefinition description DefaultListableBeanFactory with storage, the ability to manipulate BeanDefinition of .DefaultListableBeanFactory through inheritance also has a storage operation of Bean Features.

Summary :

  1. BeanFactory system, the interface is clear, perfectly embodies the principle of separation of interface.
  2. BeanFactory storage system, there are two, one is BeanDefinition storage, and the other is Bean store.
  3. DefaultListableBeanFactory as the most commonly used container class. Not only has the function BeanDefinition store operations, but also through inheritance Bean store operations function has a DefaultListableBeanFactory the stored BeanDefinition Bean created and stored by a certain algorithm .

BeanDefinition, BeanFactory, Bean the relations between

We look at their relations by understanding each interface, the class above.

We BeanDefinition to inject a BeanFactory container. BeanFactory stored to help us when we want to get a Bean when .BeanFactory help us to create BeanDefinition Bean. And cached. The creation process is participatory .

  • BeanFactory saved BeanDefiniton and Bean.
  • Bean BeanFactory with the use of functions to create BeanDefinition
  • BeanFactory allows the user to generate BeanDefinition intervention Bean features.

Extension points in the BeanFactory

Principles of design principles of opening and closing said particularly good: that a software entity should be achieved by extending the change.
Many excellent framework extension points have similar design, such as columns:

  • Tomcat in the Filter
  • pipline-valve Tomcat in
  • springmvc the Interceptor

BeanFactory from BeanDefinition to Bean was not done at once. There is a process .spring exactly leave extension points in the process. BeanDefinition to Bean to achieve a variety of custom process changes.

From the above we know: AbstractAutowireCapableBeanFactory There is a doCreateBean () method is the BeanDefinition to create Bean Next we look at this method which extension point..:

  1. xxxAware Interface: Bean can be obtained so as to achieve a column xxx BeanFactoryAware interface class can be acquired BeanFactory load the Bean during loaded in the Bean.
  2. BeanPostProcessor: BeanPostProcessor interface methods defined by two, respectively, before and after the execution of the initialization method performed bean (the InitializingBean interface or init-method defined)
  3. InitializingBean Interface: InitializingBean class that implements the interface, perform afterPropertiesSet
  4. Custom init-method method:

to sum up:

BeanFactory materials and finished goods warehouse is, and with the production line, the production cost BeanDefinition Bean. BeanFactory leave a lot of extension points, to facilitate developers can participate in the process of creating BeanDefinition to Bean's.

I welcome everyone's attention number of public action [source], the latest personal understanding timely promotions.
Here Insert Picture Description

Guess you like