[java] view plain copy
print?
- @Service
- public 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;
- }
- }
@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; }}
[java] view plain copy
print?
- 或者
- @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; }}
[java] view plain copy
print?
- 或者直接
- @Autowired
- ApplicationContext context;
或者直接 @Autowired ApplicationContext context;
原文地址:https://www.cnblogs.com/jpfss/p/8422508.html
时间: 2024-11-05 21:47:05