ApplicationContextAware如何在tomcat启动时执行set方法
请问各位大神,我有个类实现ApplicationContextAware接口,我想在tomcat启动后,就能拿到context的所有bean,该如何配置啊?
@Component
public class ContextInitializer implements ApplicationContextAware {
private static ApplicationContext context;//声明一个静态变量保存
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
ContextInitializer.context=context;
//==========================测试里面是否都有实例的bean===============
String[] beannames=context.getBeanDefinitionNames();
for(int i=0;beannames!=null&&i<beannames.length;i++){
System.out.println("bean names is "+i+" ="+beannames[i]);
}
//==========================测试========================
}
public static ApplicationContext getContext(){
return ContextInitializer.context;
}
}
我在xml里面配置了<bean id="contextInitializer" class="cn.com.de.util.ContextInitializer" lazy-init="false"/>
发现只有在浏览器访问,才会执行setApplicationContext方法,我的现在的需求是,希望tomcat启动完成,就执行setApplicationContext方法,请问该如何实现啊?谢谢大家了,感激不尽了
------解决方案--------------------WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext())这个就是context了