1. ContextLoader provided by Spring
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();wac.getBean(beanID);
This method does not depend on servlets and does not require injection. However, it should be noted that when the server starts and the Spring container is initialized, the Spring container cannot be obtained through this method
2. Implement the interface ApplicationContextAware
Define tool classes
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; //Spring application context environment/** * Implement the callback method of the ApplicationContextAware interface and set the context environment* @param applicationContext * @throws BeansException */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * Get object* @param name * @return Object An instance of a bean registered with the given name* @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * Get an object of type requiredType* If the bean cannot be converted by type, the corresponding exception will be thrown (BeanNotOfRequiredTypeException) * @param name bean registration name * @param requiredType Return object type * @return Object Return requiredType object* @throws BeansException */ public static Object getBean(String name, Class requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } /** * If the BeanFactory contains a bean definition that matches the given name, return true * @param name * @return boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * Determine whether the bean definition registered with a given name is a singleton or a prototype. * If the bean definition corresponding to the given name is not found, an exception will be thrown (NoSuchBeanDefinitionException) * @param name * @return boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * @param name * @return Class The type of the registered object* @throws NoSuchBeanDefinitionException */ public static Class getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } /** * If the given bean name has aliases in the bean definition, these aliases will be returned* @param name * @return * @throws NoSuchBeanDefinitionException */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); }}3. Configure beans
<!-- SpringContextUtil get bean through code --><bean id="SpringContextUtil "/>
Summarize
The above are several ways Spring gets beans in the code. I hope the content of this article will be helpful to everyone's study or work. If you have any questions, you can leave a message to communicate.