Spring principle
It is a comprehensive one-stop solution for enterprise application development, which runs through the presentation layer, business layer, and persistence layer. But Spring can still integrate seamlessly with other frameworks.
Spring features
1. Lightweight
2. Inversion of Control
3. Face the aspect
4. Container
5. Frame Collection
Spring core components
Spring common modules
Spring main package
Spring common annotations
Spring IOC principle
concept
Spring describes the dependency relationship between Bean and Bean through a configuration file, instantiates Bean and establishes the dependency relationship between Beans by using the reflection function of Java language. Spring's IoC container also provides advanced services such as Bean instance caching, life cycle management, Bean instance proxying, event publishing, and resource loading on the basis of completing these underlying tasks.
Spring container high-level view
When Spring starts, it reads the Bean configuration information provided by the application, and generates a corresponding Bean configuration registry in the Spring container, and then instantiates the Bean based on this registry, and assembles the dependencies between the Beans for the upper application Provide a ready operating environment. The Bean buffer pool is implemented by HashMap
IOC container implementation
BeanFactory-Framework Infrastructure
BeanFactory is the infrastructure of the Spring framework and is oriented towards Spring itself; ApplicationContext is oriented towards developers who use the Spring framework. In almost all applications, we directly use ApplicationContext instead of the underlying BeanFactory.
ApplicationContext for developing applications
ApplicationContext is derived from BeanFactory and provides more practical application-oriented functions. ApplicationContext inherits the HierarchicalBeanFactory and ListableBeanFactory interfaces. On this basis, it also extends the functions of BeanFactory through a number of other interfaces:
WebApplication architecture
WebApplicationContext is specially prepared for Web applications, it allows to load configuration files from a path relative to the Web root directory to complete the initialization work. The ServletContext reference can be obtained from the WebApplicationContext, and the entire Web application context object will be placed as an attribute in the ServletContext so that the Web application environment can access the Spring application context.
Instantiate
Spring Bean life cycle
Instantiate a Bean, which is what we often call new.
IOC dependency injection
Configure the instantiated Bean according to the Spring context, that is, IOC injection.
setBeanName implementation
If this Bean has implemented the BeanNameAware interface, the setBeanName(String) method it implements will be called, and what is passed here is the id value of the Bean in the Spring configuration file
BeanFactoryAware implementation
If this Bean has implemented the BeanFactoryAware interface, it will call the setBeanFactory it implements. SetBeanFactory(BeanFactory) passes the Spring factory itself (you can use this method to get other Beans, just configure an ordinary Bean in the Spring configuration file. )
ApplicationContextAware implementation
If this Bean has implemented the ApplicationContextAware interface, it will call the setApplicationContext(ApplicationContext) method and pass in the Spring context (the same way can also achieve the content of step 4, but it is better than 4, because ApplicationContext is a subinterface of BeanFactory, there are more Implementation method)
postProcessBeforeInitialization interface implementation-initialization preprocessing
If the Bean is associated with the BeanPostProcessor interface, the postProcessBeforeInitialization(Object obj, String s) method will be called. BeanPostProcessor is often used to change the content of the Bean, and because this is the method that is called at the end of the Bean initialization, it can also be called Applied to memory or cache technology.
init-method
If the bean is configured with the init-method attribute in the Spring configuration file, its configured initialization method will be automatically called.
postProcessAfterInitialization
If this Bean is associated with the BeanPostProcessor interface, the postProcessAfterInitialization(Object obj, String s) method will be called. Note: This Bean can be applied after the above work is completed. The Bean is a Singleton, so in general, we call the Bean with the same id will be the instance with the same content address, of course, it can also be configured in the Spring configuration file Not Singleton.
Destroy expired automatic cleaning phase
When the Bean is no longer needed, it will go through the cleaning phase. If the Bean implements the DisposableBean interface, the destroy() method that it implements will be called;
destroy-method self-configuration cleanup
Finally, if the destroy-method attribute is configured in the Spring configuration of this Bean, its configured destroy method will be called automatically.
The bean tag has two important attributes (init-method and destroy-method). With them you can customize the initialization and logout methods. They also have corresponding annotations (@PostConstruct and @PreDestroy).
Friends who need this pdf document pay attention to and like