1. What can Spring do?
Spring's main purpose is to make J2EE easy to use and promote good programming habits.
The design core of the inverted control container Spring is the org.springframework.beans package, designed to work with JavaBeans. This package is generally not directly used by users, but serves as a basis for more other functional services. The next higher level abstraction is "Bean Factory". Spring bean factory is a normal factory that enables objects to be retrieved by name and manages relationships between objects. Bean factories support two object patterns: . Singleton: In this pattern, there is a shared object instance with a specific name, which is retrieved when looking up. This is the default and is most commonly used. It is an ideal pattern for stateless objects. .Prototype: In this mode, a separate object will be created each time the fetch is obtained.
2. Spring start loading and implementation method
The first one: implement operations before initializing and destroying beans by annotating the @PostConstruct and @PreDestroy methods
The second type: define init-method and destory-method methods in xml
The third type: implement InitializingBean and DisposableBean interfaces through beans
The fourth type: write a class to implement the BeanPostProcessor interface. This interface has two methods.
(1): postProcessBeforeInitialization method, call this method before the bean defined in spring is initialized before the bean defined in spring
(2): postProcessAfterInitialization method, call this method or implementation after the bean defined in spring is initialized or initialized.
InstantiationAwareBeanPostProcessor is a subinterface of BeanPostProcessor
Execute after the Spring container is loaded
From the spring listener as the entry.
org.springframework.web.context.ContextLoaderListener
Find the method to initialize spring
/** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }Enter the initWebApplicationContext method
if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, servletContext); } } ApplicationListener
1. Write a listener class that implements ApplicationListener.
import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.stereotype.Service;@Servicepublic class StartupListenerimplements ApplicationListener<ContextRefreshedEvent>{ @Override public void onApplicationEvent(ContextRefreshedEvent event) { if(event.getApplicationContext().getParent() == null)//root application context has no parent, he is the boss. { //The logical code that needs to be executed will be executed after the spring container is initialized. System.out.println("/n/n/n/n/n/n______________/n/n/n loaded /n/n_________/n/n"); } //or the following method if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")) { System.out.println("/n/n/n________/n/n/n loaded once /n/n _________/n/n"); } }}2. Set the service scan package in the configuration file (applicationContext-servlet.xml)
<!-- Register @Controller , @Service--> <context:component-scan base-package="com.test.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
3. Deploy the startup project and print out "Load" after spring is loaded
Applicationontext and webApplicationontext after using MVC will call the above method twice. How to distinguish between these two containers?
But at this time, there will be a problem. In the web project (springmvc), there will be two containers in the system, one is the rootapplication context, and the other is our own projectName-servletcontext (as a subcontainer of the rootapplication context).
In this case, the onApplicationEvent method will be executed twice. In order to avoid the problems mentioned above, we can only call the logical code after the rootapplication context is initialized. If the initialization of other containers is completed, no processing will be done. After the modified code is completed, the code will be modified.
as follows:
@Override public void onApplicationEvent(ContextRefreshedEvent event) { if(event.getApplicationContext().getParent() == null){//root application context There is no parent, it is the boss. //The logical code that needs to be executed will be executed when the spring container is initialized. } }The order of initialization is:
Constructor > @PostConstruct > InitializingBean > init-method
Summarize
The above is all about the initial analysis of Spring framework in this article, I hope it will be helpful to everyone. If you have any questions, you can leave a message at any time and the editor will reply to everyone in time. Thank you friends for your support for this site!