There are many ways to get beans in Spring, and I'll summarize it again:
The first type: Save the ApplicationContext object during initialization
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId"); Note: This method is suitable for stand-alone applications using the Spring framework, and the program requires the program to manually initialize Spring through configuration files.
The second type: Get ApplicationContext object through the tool class provided by Spring
import org.springframework.web.context.support.WebApplicationContextUtils;ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);ac1.getBean("beanId");ac2.getBean("beanId"); illustrate:
1. These two methods are suitable for B/S systems using Spring framework, obtain the ApplicationContext object through the ServletContext object, and then obtain the required class instance through it;
2. The first method throws an exception when the acquisition fails, and the second method returns null.
The third type: inherited from the abstract class ApplicationObjectSupport
Note: The getApplicationContext() method provided by the abstract class ApplicationObjectSupport can easily obtain the ApplicationContext instance, and then obtain the bean in the Spring container. When Spring is initialized, the ApplicationContext object will be injected through the setApplicationContext(ApplicationContext context) method of the abstract class.
Fourth type: Inherited from abstract class WebApplicationObjectSupport
Note: Similar to the above method, get the WebApplicationContext instance by calling getWebApplicationContext();
Fifth type: Implementing the interface ApplicationContextAware
Description: 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.
Although Spring provides the last three methods to implement inheritance or implementation of corresponding classes or interfaces in ordinary classes to obtain Spring's ApplicationContext objects, when using it, you must pay attention to inheriting or implementing these abstract classes or interfaces. Or the obtained ApplicationContext object will be null.
The following shows how to obtain beans in Spring containers by implementing the interface ApplicationContextAware:
First, customize a class that implements the ApplicationContextAware interface and implement the methods inside:
package com.ghj.tool;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class SpringConfigTool implements ApplicationContextAware {// extends ApplicationObjectSupport{ private static ApplicationContext ac = null; private static SpringConfigTool springConfigTool = null; public synchronized static SpringConfigTool init() { if (springConfigTool == null) { springConfigTool = new SpringConfigTool(); } return springConfigTool; } public void setApplicationContext(ApplicationContext applicationContext)throws BeansException { ac = applicationContext; } public synchronized static Object getBean(String beanName) { return ac.getBean(beanName); }} Secondly, configure it in the applicationContext.xml file:
Copy the code as follows:<bean id="SpringConfigTool"/>
Finally, you can get the corresponding bean in the Spring container through the following code:
The code copy is as follows: SpringConfigTool.getBean("beanId");
Note that when the server starts the initialization of the Spring container, the Spring container cannot be obtained through the following methods:
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean(beanID);
The above is all about this article, I hope it will be helpful to everyone's learning.