Spring is a lightweight inversion of control (IoC) and tangent-oriented (AOP) container framework. How to get Spring configured beans in a program?
Bean factory (com.springframework.beans.factory.BeanFactory) is the core interface of the Spring framework. It provides advanced IoC configuration mechanism. BeanFactory makes it possible to manage different types of Java objects. The application context (com.springframework.context.ApplicationContext) is based on BeanFactory and provides more application-oriented functions. It provides international support and framework event system, making it easier to create practical applications. We generally call BeanFactory an IoC container, and ApplicationContext an application context. But sometimes for the sake of writing convenience, we also call ApplicationContext Spring container.
For the purposes of the two, we can simply divide them: BeanFactory is the infrastructure of the Spring framework, aimed at Spring itself; ApplicationContext is for developers who use the Spring framework. In almost all application occasions, we directly use ApplicationContext instead of the underlying BeanFactory.
There is a significant difference between the initialization of ApplicationContext and BeanFactory: when the BeanFactory initializes the container, it does not instantiate the bean until the target bean is accessed for the first time; while ApplicationContext instantiates all single instance beans when initializing the application context. Therefore, the initialization time of ApplicationContext will be slightly longer than that of BeanFactory
This article does not involve automatic injection through @Resource and @Autowired, and only obtains beans in the Spring configuration file through ApplicationContext.
To get the bean configured in XML, the most important thing is to get org.springframework.context.ApplicationContext
The first method to get ApplicationContext:
import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");or
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");Instantiating applicationContext in this way is very time-consuming. This way is suitable for stand-alone applications using the Spring framework. It is only recommended to use when the program needs to manually initialize Spring through configuration files. The main implementation classes of ApplicationContext are ClassPathXmlApplicationContext and FileSystemXmlApplicationContext. The former loads configuration files from the classpath by default, and the latter loads configuration files from the file system by default.
example:
public class BeanManager {private static ApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml") ;public static Object getBean(String beanId) {return context.getBean(beanId);}}Write a servlet in web.xml, start automatically, and call BeanManager in the init method
init() throws ServletException {BeanManager bm = new BeanManager();//Optional, so that spring loads the bean configuration when the web application is started. // Otherwise, it will be loaded when the BeanManager is called for the first time, affecting the speed once. }Use BeanManager.getBean(String beanId); in java code to obtain bean instance.
The second method to obtain ApplicationContext: Get ApplicationContext object through the tool class provided by Spring. It is a method specially customized for web projects. It is recommended to use it in web projects. For example:
ServletContext servletContext = request.getSession().getServletContext();ApplicationContext ac1 = WebApplicationContextUtils .getRequiredWebApplicationContext(ServletContext sc)ApplicationContext ac2 = WebApplicationContextUtils .getWebApplicationContext(ServletContext sc)ac1.getBean("beanId");ac2.getBean("beanId");Get the ApplicationContext instance object through javax.servlet.ServletContext, which means that request, session, etc. must be used.
In this way, the ApplicationContext object cannot be set as a member variable. In each specific method, you need to obtain the ServletContext through request, session, etc. and then obtain the ApplicationContext instance.
Therefore, this method is only recommended in a case where you can get the ServletContext object and you do not need to define the ApplicationContext object as a member variable.
Note: When using WebApplicationContextUtils to get the ApplicationContext instance, you need to add the org.springframework.web.context.ContextLoaderListener listener in the web.xml configuration file, otherwise the ApplicationContext object cannot be obtained and Null is returned.
Configuration file: web.xml
<!--ContextLoaderListener automatically injects applicationContext, and gets the --><!--Spring configuration file loading location--><context-param><param-name>contextConfigLocation</par am-name><param-value>/WEB-INF/spring/appContext.xml,/WEB-INF/spring/appInterceptor.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
3. Inherited from abstract class ApplicationObjectSupport
The abstract class ApplicationObjectSupport provides the getApplicationContext() method, which can easily obtain the ApplicationContext. When Spring is initialized, the ApplicationContext object will be injected through the setApplicationContext(ApplicationContext context) method of the abstract class.
4. Inherited from abstract class WebApplicationObjectSupport
Use getWebApplicationContext() to get org.springframework.web.context.WebApplicationContext because Web applications have more features than ordinary applications, WebApplicationContext extends ApplicationContext. WebApplicationContext defines a constant ROOT_WEB_APPLICATION_ CONTEXT_ATTRIBUTE. When the context is started, the WebApplicationContext instance is placed in the attribute list of ServletContext with this key. Therefore, we can directly obtain the WebApplicationContext from the Web container through the following statement:
WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
5. Implement the interface ApplicationContextAware
Implement the setApplicationContext(ApplicationContext context) method of this interface and save the ApplicationContext object. When Spring is initialized, the ApplicationContext object is injected through this method.
The third, fourth and fifth methods all require configuring the class in the Spring configuration file:
<!--Assuming that ApplicationContextTool is a specific implementation class that inherits or implements the third, fourth and fifth methods--><bean></bean>
Otherwise, the ApplicationContext will not be retrieved and Null will be returned.
The above content introduces a summary of the Spring bean method in Java, I hope you like it.