Summary of the 5 commonly used methods for Java classes to obtain beans in spring containers : Method 1: Save the ApplicationContext object code
during initialization : ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId" ); Description: This method is suitable for independent applications using the Spring framework, and the program needs to manually initialize Spring through the configuration file. Method 2: Obtain the ApplicationContext object code 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"); Description:
This method is suitable for the B/S system that adopts the Spring framework, obtains the ApplicationContext object through the ServletContext object, and then obtains the required class instance through it.
The difference between the above two tools is that the former throws an exception when the acquisition fails, and the latter returns null.
Method 3: Inherit from the abstract class ApplicationObjectSupport Description: 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.
Method 4: Inherited from the abstract class WebApplicationObjectSupport Description: Similar to the above method, call getWebApplicationContext() to obtain the WebApplicationContext
Method 5: Implement the interface ApplicationContextAware Description: Implement the setApplicationContext (ApplicationContext context) method of this interface and save the ApplicationContext object. When Spring is initialized, it will inject the ApplicationContext object through this method.
Although spring provides the last three methods to inherit or implement corresponding classes or interfaces in ordinary classes to obtain Spring's ApplicationContext objects, it is necessary to pay attention to ordinary java classes that implement these classes or interfaces when using them. Configure in Spring's configuration file application-context.xml file. Otherwise the obtained ApplicationContext object will be null.
The following is an example of my implementation of the ApplicationContextAware interface
public synchronized static Object getBean(String beanName) { return context.getBean(beanName); }
}
The configuration information in the XML file
finally provides a It does not depend on servlet and does not require injection. Note that when the server starts and the Spring container is initialized, the Spring container cannot be obtained by the following methods. For details, you can view the source code org.springframework.web.context.ContextLoader