【SpringBoot动态加载Spring容器的类】 博客分类: 编程语言--JAVA之SpringSpring-Boot
程序员文章站
2024-03-20 19:34:16
...
package com.xxx import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.io.Serializable; /** * SpringBoot动态加载Spring容器的类 * 类似JalorApplicationContext */ @Component public class ApplicationContextUtil implements ApplicationContextAware, Serializable { /** * 上下文 */ private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } public static ApplicationContext getApplicationContext() { return context; } public static <T> T getBean(Class<T> beanClass) { return context.getBean(beanClass); } }