①. What is a bean factory?
- ①. According to the above schematic diagram of Spring, the whole framework of Spring is actually the process of making things in the factory. If you analyze what the factory does, the whole framework of Spring will be clear
- ②. When you enter BeanFactory, you can see the core words:
Summarize the words in the source code: Prototype mode returns Prototype type beans, while singleton mode returns singleton beans
/** * The root interface for accessing a Spring bean container. * Root interface, the entry to the entire access container * <p>This is the basic client view of a bean container; * further interfaces such as {@link ListableBeanFactory} and * {@link org.springframework.beans.factory.config.ConfigurableBeanFactory} * are available for specific purposes. * <p>This interface is implemented by objects that hold a number of bean definitions, * This interface is an implementation that stores a lot of BeanDefinition information with a unique name * each uniquely identified by a String name. Depending on the bean definition, * the factory will return either an independent instance of a contained object * (the Prototype design pattern), or a single shared instance (a superior * The factory will return a separate instance or a shared instance (prototype mode, singleton mode) * alternative to the Singleton design pattern, in which the instance is a * singleton in the scope of the factory). **/
②. Analysis in BeanFactory class
-
①. BeanFactory(Bean factory). A factory is nothing more than a simple factory, an abstract factory and a factory method
-
②. First, the method exposed in the BeanFactory factory is called getBean(), which is to obtain components
The most important thing of the factory is to create components, and then return an object of type T, which only creates bean s
<T> T getBean(Class<T> requiredType) throws BeansException;
-
③. It's not easy to build a pattern factory in the bottom layer. We can only use a pattern factory in the bottom layer. It's not a complex method. We can only use the pattern factory in the bottom layer
-
④. BeanFactory(Bean's factory) has three factories: commonly known as branch factory
③. Hierarchalbeanfactory (factory of inheritance tree)
-
①. It is mainly used to define parent-child factories (parent-child containers)
getParentBeanFactory(): get its parent factory
containsLocalBean(): judge whether the package in the factory does not contain some local beans
-
②. In spring, there are abstract factories under BeanFactory(Bean factory), but these abstract factories do not have a method to create products. They are equivalent to a product line and a product. An abstract factory is only equivalent to enhancing the function of the factory, which has nothing to do with the product, so these abstract factories are not established
-
③. The purpose of establishing an abstract factory is: if there are branches under the beanfactory to manufacture products, there will be other plans for product interfaces
④. Listablebeanfactory interface details
- ①. It mainly saves the core information in the ioc container
getBeanDefinitionCount(): get the sum of bean definition information
getBeanDefinitionNames(): get the names of all bean definition information
getBeanNamesForType(): you can also get bean names by type
-
②. See the structure chart below for details
The following DefaultListableBeanFactory is our archive
AbstractApplicationContext is an environment class
-
③. Explain in detail how AbstractApplicationContext is a bad environment class
- Next interface under ResourceLoader resource reader = = ResourcePatternResolver
- Abstract IOC container (AbstractApplicationContext) has the interface of resource pattern resolver
- And it is obtained as soon as the object is created. It is obtained by using the getResourcePatternResolver method
- This reveals that the environment class of a policy pattern is AbstractApplicationContext
- That is, the IOC container gets the resource parser. As long as the resource parser is transmitted differently, the IOC container can get resources from different places
- ④. Annotation config ApplicationContext (IOC) container - > configurableapplicationcontext - > ApplicationContext implementation - > listablebeanfactory
The IOC container we use also has the function provided by listablebean factory, which can list all beans
- ⑤. When you come to the AbstractApplicationContext implementation, you can also see where the definition information of a bean exists
- We use the abstract IOC container (AbstractApplicationContext), which also has a generic ApplicationContext
- The GenericApplicationContext class contains a DefaultListableBeanFactory,
- The parent class GenericApplicationContext of the IOC container we use has a child attribute in the parent class GenericApplicationContext, but this child attribute is called DefaultListableBeanFactory, which is combined
- Now there is another combination relationship: GenericApplicationContext combines a DefaultListableBeanFactory
- ⑥. What is the use of combining DefaultListableBeanFactory?
- DefaultListableBeanFactory contains some maps, serializablefactories. This Map introduces its own factory, combination mode, or combination itself. Self combination will form the inheritance tree of the whole factory. Each factory will have its unique ID. use a Map, that is, there is only one factory in Spring, In fact, there can be countless factories in Spring, and each factory can make something.
- The advantage of this is that the beans made between factories can be isolated, so sometimes some beans in the environment don't want others to reference, but you can reference multiple factory patterns only within its scope. Of course, up to now, you haven't seen multiple factories in any project, but there are definitions in the bottom layer of Spring
/** Map from serialized id to factory instance. */ //What is introduced here is your own factory, combination mode, or combination. If you combine yourself, you will form the inheritance tree of the whole factory private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =new ConcurrentHashMap<>(8);
/** List of bean definition names, in registration order. */ //Bean definition names: the name of the bean definition. Here is the name of the saved bean definition, //That is, the names of all bean definitions stored in the database private volatile List<String> beanDefinitionNames = new ArrayList<>(256); /** Map of singleton-only bean names, keyed by dependency type. */ //ingletonBeanNamesByType: single instance is saved by type, which explains that the spring bottom layer can find groups by type //An underlying pool of components is actually taken in the map, that is, after the component is created, a type is passed, and then the corresponding type is obtained from the map //The name of the component, and then get the component by name from the ioc container. private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64); /** Map of singleton and non-singleton bean names, keyed by dependency type. */ //allBeanNamesByType: find all beans according to this type private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64); /** Map of bean definition objects, keyed by bean name. */ //beanDefinitionMap: the collection of all bean definition information is saved according to the name and the corresponding BeanDefinition relationship, //This also explains that in the Spring architecture principle, all resources parsed by Spring will exist in this archive, which is one //beanDefinitionMap private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
⑤. DefaultListableBeanFactory Archive
-
①. You can see whether DefaultListableBeanFactory is BeanDefinitionRegistry(Bean definition information registry), simpleliasregistry (alias Registry), singletonbeanregistry (single instance bean Registry), or BeanFactory(Bean factory), so this is the general Archive (DefaultListableBeanFactory)
-
②. Although the DefaultListableBeanFactory of the general archives does not form a parent-child relationship with the IOC container, it forms a composite relationship (the IOC container combines the whole DefaultListableBeanFactory of the General Archives)
-
③. DefaultListableBeanFactory plays a crucial role in Spring
⑥. AutowireCapableBeanFactory
-
①. It provides automatic assembly capability, that is, BeanFactory also has an automatic assembly factory, which only extends some functions
-
②. All auto assembly functions are defined by AutowireCapableBeanFactory, but there are many final implementations to implement this method
-
③. AnnotationApplicationContext has no automatic assembly capability, but it combines (DefaultListableBeanFactory) archives (it has automatic assembly capability), so AnnotationApplicationContext also has automatic assembly capability.