IOC source code analysis

Table of contents

Mainly analyzed from 3 aspects

Bean and BeanDefinition

The main things to do when the container is initialized (main context)

BeanFactory

ApplicationContext

template method pattern

Relationship between Resource, ResourceLoader, and container

BeanDefinitionReader

Registration of BeanDefinition

summary


  • Mainly analyzed from 3 aspects

    • parse configuration
    • Locating and registering objects
    • inject object
  • Bean and BeanDefinition

  • Beans are first-class citizens of Spring
    • The essence of a bean is a java object, but the life cycle of this object is managed by the container
    • There is no need to add any additional restrictions on the original java class in order to create a bean (low intrusion)
    • The control method of java objects is reflected in the configuration
    • A bean is an object instantiated, assembled and managed by the Spring IoC container
    • Bean is not edited by programmers, but generated by Spring through reflection when the program is running
  • What is BeanDefinition
    • According to the configuration, generate the BeanDefinition used to describe the Bean
    • BeanDefinition is the configuration metadata interface that defines Bean
    • Include:
    • Bean's class name
    • Set the name of the parent bean, whether it is primary
    • Bean behavior configuration information, scope, automatic binding mode, life cycle callback, lazy loading, initial method, destruction method, etc.
    • Dependency settings between beans, dependencies
    • Construction parameters, property settings
    • example:
    • Scope of action scope (@Scope)
    • Lazy loading lazy-init (@Lazy): Determine whether the Bean instance is lazy loaded
      • true: The bean instance will only be created when the bean instance is used
    • Preferred primary (@Primary): The bean set to true will be the preferred implementation class
      • When an interface has multiple implementation classes, the bean with @Primary will be the preferred implementation class
    • factory-bean和factory-method(@Configuration和@Bean)
      • factory-bean: the name of the factory bean
      • factory-method: the name of the factory method
  • The main execution is mainly to call getBean from the container and pass in the id of the bean to obtain the instance object
  • Different BeanIds create different instances
  • Regardless of the way of xml or annotation, the bean object will be read into the memory by the container location, and then parsed into beandefinition instances one by one and registered in the container. The whole process occurs during the initialization process of the container
  • The main things to do when the container is initialized (main context)

  • The inheritance relationship of Spring's Bean is not implemented through extends and implements, but by setting the parent attribute

  • BeanDefinition: Describes the configuration information of a Bean instance (delayed loading, scope...)
  • AttributeAccessor: Defines the most basic modification or acquisition method for metadata of any object, mainly used to obtain BeanDefinition attributes and operate on these attributes
  • BeanMetadataElement: Used to transmit configurable meta-objects, mainly used to return the BeanDefinition class object itself
  • AbstractBeanDefinition: Defines a common constructor, and subclasses can assign values ​​​​to attributes based on the constructor. Secondly, it defines get and set methods for some general attributes, which is convenient for assigning values ​​​​to general attributes, and also provides public tool methods
  • RootBeanDefinition: It cannot be used as a subclass of other classes, and is usually used to accept information merged by multiple BeanDefinitions at runtime; it can accept the attributes of two BeanDefinitions with an inheritance relationship, and undertake the combination of the two together except for the parent attribute. Attributes
  • GenericBeanDefinition: It is a generic BeanDefinition implementation with a parentName attribute, which is convenient for programs to set parentBeanDefinition at runtime
  • BeanFactory

  • What is the difference between BeanFactory and FactoryBean?
    • BeanFactory is the root interface of the Spring container, which defines the most basic functional characteristics of the Bean factory (such as obtaining Bean instances according to BeanName, etc.)
    • Used as a container for managing beans, the beans generated in Spring are all managed by the implementation class of this interface
    • FactoryBean is also an interface. Based on the getObject method in the interface, users can generate a set of complex logic to generate beans
    • It is also a Bean in essence, but it is not injected into a certain place such as Service
    • Its function is to generate ordinary beans. After implementing this interface, the Spring container will take out the beans that implement this interface during initialization, and use the getObject method in the Bean to generate the beans we want.
  • example:
  • Configure the bean and test to get the bean instance

  • It was found that the instance of UserFactoryBean was not created, but the instance of user
  • Explain that calling the getBean method of UserFactoryBean directly, it will call the getObject method inside by default, and return the created user instance
  • So how to get an instance of UserFactoryBean?
  • Just add an & in front

  • Important methods of BeanFactory:

  • simple container

  • The interface is mainly used to describe the functions of the container, and the implementation class realizes the function
  • ListableBeanFactory: List the information of the instances produced by the factory in batches (beanName)
  • ApplicationContext

  • Terminology Supplement
    • Component scanning: automatically discover the beans that need to be created in the application container
    • Autowiring: Automatically satisfy dependencies between beans (dependency injection on member variables marked with Autowired annotations)
  • BeanFactory is for Spring itself, and ApplocationContext is for developers
  • For example, the Spring container is a car, the BeanFactory is the engine of a car, and the ApplicationContext is a complete car

  • ApplicationContext: application context, inheriting the BeanFactory interface, it is a more advanced container of Spring, providing more useful functions
    • Internationalization (MessageSource)
    • Access resources such as URLs and files (ResourceLoader)
    • Load multiple (inherited) contexts, each focusing on a specific layer, such as the application's web layer
    • Message sending and response mechanism (ApplicationEventPublisher)
    • AOP (Interceptor)
  • Because it inherits multiple interfaces, it has more functions
    • EnvironmentCapable: You can get the contextConfigLocation information in web.xml through Environment getEnvironment() in EnvironmentCapable, and load all configuration files of Spring according to its value
    • ListableBeanFactory: manage beans by list
    • HierarchicalBeanFactory: Implement multi-level containers to manage each layer of beans
    • ResourcePatternResolver: load resource files
    • MessageSource: manage message, and then realize the function of internationalization
    • ApplicationEventPublisher: has the ability to publish events, (the container will publish some listeners when it starts to listen to the published events) monitoring mechanism
  • ApplicationContext commonly used containers
  • Traditional XML-based configuration for classic containers
    • FileSystemXmlApplicationContext: load configuration from the file system
    • ClassPathXmlApplicationContext: load configuration from classpath
    • XmIWebApplicationContext: container for web applications
  • Currently more popular containers
    • AnnotationConfigServletWebServerApplicationContext: under the Boot module of SpringBoot
    • AnnotationConfigReactiveWebServerApplicationContext: used to meet responsive requirements
    • AnnotationConfigApplicationContext: commonly used for common non-web applications
  • The ApplicationContext methods all start with get and are read-only. You need to use sub-interfaces to realize the configurable capabilities of ApplicationContext
  • Subinterface ConfigurableApplicationContext:
    • It provides some methods to configure the ApplicationContext, the ability to start, refresh, and close the application context
    • Restart the container, clear the cache, and reload the class information
  • AbstractApplicationContext:
    • Realize the simple and unmovable parts in ApplicationContext: container factory processing, event sending and broadcasting, listener registration, container initialization operation refresh method, getBean method
  • refresh() is the core method of Spring. It is executed when SpringApplication.run(args). It is a synchronous synchronization method, which is implemented with the synchronized keyword
  • The general function of refresh()
    • Container initialization, configuration analysis
    • Registration and activation of BeanFactoryPostProcessor and BeanPostProcessor
    • Internationalization configuration
  • template method pattern

  • Around the abstract class, realize the general logic, define the template structure, and part of the logic is implemented by the subclass
  • Based on inheritance, an abstract class will be prepared, part of the logic will be implemented with specific methods and specific logic, and then a template structure will be defined, and the rest of the specific content will be delayed to subclasses for implementation.
  • Declaring some abstract methods forces subclasses to implement the rest of the logic
  • Reuse: Reuse the code of the same logic in the parent class, and sink the specific implementation to the subclass
  • Reverse control: Call the operation of the subclass through the parent class, and extend different behaviors through the specific implementation of the subclass to achieve reverse control
  • Customized behaviors are implemented through subclass extensions, in line with the principle of opening and closing

  • Template method: defines the skeleton of the business that the entire method needs to implement
  • Specific method: some certain and invariable logic, directly implemented by the parent class
  • Hook: not directly called by the subclass but by the caller of the abstract class when a specific condition occurs to respond to the event that occurs
  • Hooks are keys for subclasses to be flexible
  • For example: Going to KTV to sing, the waiter helps to turn on the stereo, and after the end, the customer pays is a unified and necessary operation, so it is implemented in the parent class. Ordering songs depends on the user's needs, so it is set as an abstract method and handed over to the subclass for implementation. But whether additional subclasses will be consumed or not depends on the situation. Selective implementation is defined as a hook method

  • The refresh() method is a template method that mainly defines what needs to be done when the container starts. The method:

  • Relationship between Resource, ResourceLoader, and container

  • Resources in java will be abstracted into urls, and the protocol of urls will be parsed to handle resources of different protocols
  • And Spring abstracts physical resources into Resource
  • Resource
    • An interface that defines the basic operations of a resource

    • InputStreamSource: There is only one method to get the resource stream
    • Resource family:

    • There are different classes of implementations for different resources; each implementation class represents the resource's access strategy
    • EncodedResource: encoding processing of resource files
    • AbstractResource: Most of the default public implementations of the Resource method; if you want to customize the Resource, you can inherit it and override the corresponding method
    • ServletContextResource: implemented by accessing context resources in the web container, supports access in the form of streams and urls, and can also access resources from jar packages
    • ClassPathResource: Access resources under the class loading path; automatically search for resources under WEB-INF/classes
    • FileSystemResource: Access file system resources; the File class provided by java can also be implemented
    • The correct Resource will be automatically selected according to the resource address
    • Powerful way to load resources
    • Automatically identify resource address prefixes such as "classpath:" and "file:"
    • Supports automatic parsing of Ant-style resource addresses with wildcards
    • Ant
    • Path matching expressions are used to match URIs; similar to regular expressions, except that the scope of regular expressions is relatively wide, and Ant is only suitable for path matching
    • ? matches any single character
    • * matches 0 or any number of characters
    • ** matches 0 or more directories
    • example:

  • Resourceloader
    • Implement different Resource loading strategies and return specific types of Resource on demand
    • It is an interface to obtain resources according to the path; it can be classPath or file, etc.

  • DefaultResourceLoader
    • The simple factory pattern focuses on: returning the created object, the user does not understand the object itself, which is equivalent to a black box
    • But the strategy pattern requires the user to understand the strategy itself, that is, what kind of Resource loading should be used for what kind of resources

    • Because the method in Resourceloader can only get one resource instance, another interface ResourcePatternResolver comes

    • Implement the class PathMatchingResourcePatternResolver to implement, and also support the Ant path style pattern
    • Spring provides Resource and ResourceLoader to unify and abstract the entire resource and its location, making the boundary between resource and resource location clearer, and there is DefaultResourceLoader to make custom implementation clearer and more convenient
    • It is found that ApplicationContext inherits ResourcePatternResolver and then indirectly inherits ResourceLoader
    • So any implementation of ApplicationContext can be seen as an instance of ResourcePatternResolver or ResourceLoader
    • The implementation class of the entire ApplicationContext can fully support ResourcePatternResolver and ResourceLoader, which is why advanced containers support uniform loading of resources
    • When the container reads the configuration, it is delegated to PathMatchingResourcePatternResolver and DefaultResourceLoader to execute
  • BeanDefinitionReader

  • It is the user of ResourceLoader, the user of the resource loading tool
  • It uses ResourceLoader and ResourcePatternResolver to parse configuration information into BeanDefinitions, and register BeanDefinitions into containers with BeanDefinitionRegistry
  • effect:
  • Defines a series of interfaces for loading BeanDefinition, for the loading of single or multiple configuration files, or the loading of a single resource instance or multiple resource instances, the ultimate goal is to convert the configuration of the configuration file into individual BeanDefinitions

  • Architecture:

  • AbstractBeanDefinitionReader
    • Implemented the common logic of BeanDefinition
    • If it is an instance of ResourcePatternResolver, it means that multiple resources need to be loaded
    • Regardless of loading a single or multiple resources, the loadBeanDefinitions method will be called for further loading at the end
    • loadBeanDefinitions(): mainly determines whether to load single or multiple resources based on the type of resource loader provided by the user
    • Implementation classes are defined for different resource types
  • XmlBeanDefinitionReader
    • Pass in the path of the specified xml file, XmlBeanDefinitionReader calls its parent class ResourceLoader to return the resoure instance according to the path passed in, and then calls the loadBeanDefinitions method to execute

  • Registration of BeanDefinition

  • method:
  • 1---Register a new BeanDefinition instance to the registry

  • 2---Remove the existing instance in the registry

  • 3---Get the specified BeanDefinition instance from the registry

  • 4 --- Determine whether the BeanDefinition instance is in the registry (whether registered)

  • DefaultListableBeanFactory implements the BeanDefinitionRegistry interface

  • At the same time, the beanDefinitionMap defined in it stores the registered beanName as the key, and the registered instance as the value

  • Configure to read to load to parse
  • The main logic is to register the parsed beanDefinition instance in the DefaultListableBeanFactory container (beanDefinitionMap inside)
  • The annotation container is created before the xml file, why should it be created in advance?
  • Because he will create some system-built-in beanDefinition instances in advance, it is necessary to create a DefaultListableBeanFactory instance in the constructor in advance to provide registration for the system-built-in beanDefinition
  • First load the built-in beanDefinition instance, and then load the entrance (the bean put into the container by the Component)
  • Then load other classes modified by annotations (Controller, Service....)
  • It is registered when the container is refreshed. Like the xml configuration, it is registered when the container is refreshed.
  • The beanDefinition of BeanDefinitionRegistryPostProcessor will be executed first, and the normal BeanFactoryPostProcessor will be executed later
  • 执行BeanDefinitionRegistryPostProcessor会调用invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry)
  • invokeBeanFactoryPostProcessors(registryProcessors, beanFactory) will be called after execution; to execute ordinary BeanFactoryPostProcessor
  • summary

  • Spring will create Bean according to BeanDefinition
  • DefaultListableBeanFactory: mainly responsible for the registration of BeanDefinition
  • The commonality of container refresh: all use the refresh() in AbstractApplicationContext
  • Configuration resources will be converted into different Resource object instances in Spring, which is inseparable from the support of ResourceLoader
  • With ResourcePatternResolver, Spring can select the appropriate resource for packaging according to the ur path of the configuration resource, reflecting the strategy mode
  • With a good weapon, BeanDefinitionReader is used
  • The xml resource is parsed into a Document object by the XmlBeanDefinitionReader, and then entrusted to the BeanDefinitionDocumentReader to be parsed into a GenericBeanDefinition instance, and then registered in the DefaultListableBeanFactory built-in container
  • Different from xml, all beanDefinition instances in xml are registered when refreshing in the refresh() method, and annotations are divided into three types of BeanDefinition registration
  • The first category: the BeanDefinition instance set inside the container, which is registered in the built-in container once the constructor of the container is called
  • The second category: user-defined classes with @Configuration are registered when the register() method is called in the container's constructor
  • The third category: conventional BeanDefinition is registered when the container-level post-processor in the refresh() method is called
  • AnnotatedBeanDefinitionReader: Responsible for parsing the above three types of BeanDefinition and registering it in the DefaultListableBeanFactory container

Guess you like

Origin blog.csdn.net/weixin_59624686/article/details/130938029