Several ways for Spring to obtain beans in code---- 20160913

Method 1: Save the ApplicationContext object during initialization Method 2: Obtain the ApplicationContext object through the utils class provided by Spring Method 3: Inherit from the abstract class ApplicationObjectSupport Method 4: Inherit from the abstract class WebApplicationObjectSupport Method 5: Implement the interface ApplicationContextAware Method 6: Provided by Spring ContextLoader

Summary of the way to get beans in spring:

Method 1: Save the ApplicationContext object during initialization
  1. ApplicationContext ac =newFileSystemXmlApplicationContext("applicationContext.xml");
  2. ac.getBean("beanId");

Note: 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 through the tool class provided by Spring
  1. ApplicationContext ac1 =WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
  2. ApplicationContext ac2 =WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
  3. ac1.getBean("beanId");
  4. 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: Inherit from the abstract class WebApplicationObjectSupport

Description: Similar to the above method, call getWebApplicationContext() to get 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.

The following is the code that implements the ApplicationContextAware interface. The first two methods are similar:

  1. public class SpringContextUtil implements ApplicationContextAware {
  2. // Spring application context
  3. privatestaticApplicationContext applicationContext;
  4. /**
  5. * Implement the callback method of the ApplicationContextAware interface to set the context
  6. *
  7. * @param applicationContext
  8. */
  9. publicvoid setApplicationContext(ApplicationContext applicationContext){
  10. SpringContextUtil.applicationContext = applicationContext;
  11. }
  12. /**
  13. * @return ApplicationContext
  14. */
  15. publicstaticApplicationContext getApplicationContext(){
  16. return applicationContext;
  17. }
  18. /**
  19. * 获取对象
  20. *
  21. * @param name
  22. * @return Object
  23. * @throws BeansException
  24. */
  25. publicstaticObject getBean(String name)throwsBeansException{
  26. return applicationContext.getBean(name);
  27. }
  28. }

虽然,spring提供的后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取spring 的ApplicationContext对象,但是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件applicationContext.xml文件中进行配置。否则获取的ApplicationContext对象将为null。

方法六:通过Spring提供的ContextLoader
  1. WebApplicationContext wac =ContextLoader.getCurrentWebApplicationContext();
  2. wac.getBean(beanID);

最后提供一种不依赖于servlet,不需要注入的方式。但是需要注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器,细节可以查看spring源码org.springframework.web.context.ContextLoader。

 

原文链接:http://www.dexcoder.com/selfly/article/326

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326702486&siteId=291194637