Obtain the ApplicationContext object based on the SpringBoot platform, and manually obtain the Spring managed bean through the instance
Build project
The project in this chapter does not need too much content. Just add a Web dependency.
ApplicationContextAware
Implement ApplicationContextAware interface
package com.xunmei.api; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * Get Spring context object * */ @Component public class ApplicationContextProvider implements ApplicationContextAware { /** * Context object instance */ private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * Get applicationContext * @return */ public ApplicationContext getApplicationContext() { return applicationContext; } /** * throughGet Bean through name * @param name * @return */ public Object getBean(String name){ return getApplicationContext().getBean(name); } /** * Get Bean. class * @param clazz * @param <T> * @return */ public <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } /** * Return the specified Bean through name and Clazz * @param name * @param clazz * @param <T> * @return */ public <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
After we get the ApplicationContext object instance, we can manually obtain the injected instance object of the Bean,
In the ApplicationContextProvider class, I simply implemented several methods to obtain the specified Bean instance. Of course, you can add more methods to complete more business logic.
If you want to use ApplicationContext in a non Spring managed entity and do not want to inject ApplicationContextProvider to complete instantiation,
At this time, we can change the ApplicationContext instance object to a static instance and the method to a static method, so that the instance of the specified Bean can also be obtained externally. As follows:
@Component public class ApplicationContextProvider implements ApplicationContextAware { /** * Context object instance */ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * Get applicationContext * @return */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * Get Bean. By name * @param name * @return */ public static Object getBean(String name){ return getApplicationContext().getBean(name); } /** * Get Bean. class * @param clazz * @param <T> * @return */ public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } /** * Return the specified Bean through name and Clazz * @param name * @param clazz * @param <T> * @return */ public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }