springboot应用获取spring上下文的4种方式
实际开发中我们经常需要通过spring上下文获取一些配置信息,本文阐述springboot应用获取spring上下文的几种方式。
文章目录
方式一:实现ApplicationContextAware接口
方式二:非static方法版
方式三:在springboot引导类里设置
方式四:直接注入ApplicationContext
方式一:实现ApplicationContextAware接口
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 实现ApplicationContextAware接口设置applicationContext * 提供static方法供调用者使用,不要求使用者受spring容器管理 */ @Component public class SpringContextUtil1 implements ApplicationContextAware { public static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil1.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return SpringContextUtil1.applicationContext; } public static Object getBean(String name) { return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } }
方式二:非static方法版
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 实现ApplicationContextAware接口设置applicationContext * 如果使用者也是被spring管理的bean则可以使用注入的方式使用,而非调用static方法 */ @Component public class SpringContextUtil2 implements ApplicationContextAware { public ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public ApplicationContext getApplicationContext() { return this.applicationContext; } public Object getBean(String name) { return applicationContext.getBean(name); } public <T> T getBean(Class<T> clazz) { return applicationContext.getBean(clazz); } public <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } } @Component public class ContextUser{ /** * 使用者直接注入 */ @Autowired public SpringContextUtil2 springContextUtil2; }
方式三:在springboot引导类里设置
import org.springframework.context.ApplicationContext; /** * 在springboot引导类里设置applicationContext * 工具类无需实现ApplicationContextAware接口 */ public class SpringContextUtil3 { public static ApplicationContext applicationContext; public static ApplicationContext getApplicationContext() { return SpringContextUtil3.applicationContext; } public static void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil3.applicationContext = applicationContext; } public static Object getBean(String name) { return getApplicationContext().getBean(name); } public static Object getBean(Class<?> requiredType) { return getApplicationContext().getBean(requiredType); } public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } } //在springboot引导类里设置applicationContext @SpringBootApplication public class MainApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args); SpringContextUtil3.setApplicationContext(context); } }
方式四:直接注入ApplicationContext
@Component public class ContextUser{ /** * 在需要使用上下文的地方直接注入(前提:使用者受spring容器管理) */ @Autowired public ApplicationContext applicationContext; }
怎么样,第四种方式是不是很easy?!
赞 (0)