SpringBoot项目获取ApplicationContext
程序员文章站
2022-05-25 10:57:28
...
- 直接使用
@Autowired
注入@Autowired private ApplicationContext ctx;
- 实现
ApplicationContextAware
接口,重写setApplicationContext
方法(推荐)@Component public class ApplicationContextProvider implements ApplicationContextAware{ private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public ApplicationContext getApplicationContext() { return applicationContext; } }
- 继承抽象类
ApplicationObjectSupport
@Component public class DashboardServiceConfig extends ApplicationObjectSupport { private ApplicationContext ctx; @Override protected void initApplicationContext(ApplicationContext context) throws BeansException { super.initApplicationContext(context); ctx = context; }
上一篇: 赵云放弃一段姻缘的桂阳郡在哪里?
推荐阅读