Several ways for Spring to get beans in code

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 = new FileSystemXmlApplicationContext("applicationContext.xml");
  2. ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

Note: This method is suitable for independent applications that use 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, and obtains the ApplicationContext object through the ServletContext object. Then get the required class instance through it. The difference between the above two tools is that the former throws an exception when the acquisition fails. The latter returns null.

Method 3: Inherit from the abstract class ApplicationObjectSupport


Description: The abstract class ApplicationObjectSupport provides the getApplicationContext() method. Can easily obtain 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 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 to implement the ApplicationContextAware interface. The first two methods are similar:

 
  1. public class SpringContextUtil implements ApplicationContextAware {
  2.  
  3. // Spring application context
  4. private static ApplicationContext applicationContext;
  5.  
  6. /**
  7. * Implement the callback method of the ApplicationContextAware interface. set context
  8. *
  9. * @param applicationContext
  10. */
  11. public void setApplicationContext(ApplicationContext applicationContext) {
  12. SpringContextUtil.applicationContext = applicationContext;
  13. }
  14.  
  15. /**
  16. * @return ApplicationContext
  17. */
  18. public static ApplicationContext getApplicationContext() {
  19. return applicationContext;
  20. }
  21.  
  22. /**
  23. * get object
  24. *
  25. * @param name
  26. * @return Object
  27. * @throws BeansException
  28. */
  29. public static Object getBean(String name) throws BeansException {
  30. return applicationContext.getBean(name);
  31. }
  32. }

Although the last three methods provided by spring can be implemented in common classes to inherit or implement corresponding classes or interfaces to obtain Spring's ApplicationContext objects, it is necessary to pay attention to the common java classes that implement these classes or interfaces when using them. Configure in the Spring configuration file applicationContext.xml file. Otherwise the obtained ApplicationContext object will be null.

Method 6: Through the ContextLoader provided by Spring

 
  1. WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
  2. wac.getBean(beanID);

Finally, provide a way that does not depend on servlet and does not require injection. But need to pay attention to one thing, when the server starts. When the Spring container is initialized, the Spring container cannot be obtained through the following methods. For details, see the spring source code org.springframework.web.context.ContextLoader.

Guess you like

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