- @Service
- public class SpringManager implements ApplicationListener<ContextRefreshedEvent> {
- private static ApplicationContext applicationContext = null;
- @Override
- public void onApplicationEvent(ContextRefreshedEvent event) {
- if(applicationContext == null){
- applicationContext = event.getApplicationContext();
- }
- }
-
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
- }
@Servicepublic class SpringManager implements ApplicationListener<ContextRefreshedEvent> { private static ApplicationContext applicationContext = null; @Override public void onApplicationEvent(ContextRefreshedEvent event) { if(applicationContext == null){ applicationContext = event.getApplicationContext(); } } /*ApplicationContext context= ContextLoader.getCurrentWebApplicationContext();//尝试下这个方法*/ public static ApplicationContext getApplicationContext() { return applicationContext; }}
- 或者
- @Service
- public class SpringTool implements ApplicationContextAware {
- private static ApplicationContext context = null;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- context = applicationContext;
- }
- public static ApplicationContext getApplicationContext() {
- return context;
- }
-
-
- }
或者@Servicepublic class SpringTool implements ApplicationContextAware { private static ApplicationContext context = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static ApplicationContext getApplicationContext() { return context; }}
- 或者直接
- @Autowired
- ApplicationContext context;
或者直接 @Autowired ApplicationContext context;