Java class to get spring container bean

http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html


There are several ways to get Spring's ApplicationContext

  (2013-10-16 17:54:49)
   




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

quartzpackage.util;

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 context = null;
private static SpringConfigTool stools = null;
public synchronized static SpringConfigTool init(){
  if(stools == null){
   stools = new SpringConfigTool();
  }
  return stools;
}

public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
  context = applicationContext;
}

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

Title1 import org. springframework.web.context.ContextLoader; 
import org.springframework.web.context.WebApplicationContext; 
 
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 
  wac.getBean(beanID); 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325905731&siteId=291194637