public class SysInitListener implements ServletContextListener {
/* @Resource
private DicService dicService;*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
/*
* 注意:在自定义 listener 中如果需要使用 spring 容器中的 bean ,不可以直接 @Autowired
* (1) Listener的生命周期是由servlet容器(例如tomcat)管理的,
* 项目启动时上例中的ConfigListener是由servlet容器实例化并调用其contextInitialized方法,
* 而servlet容器并不认得@Autowired注解,因此导致ConfigService实例注入失败
*
* (2) 而spring容器中的bean的生命周期是由spring容器管理的
*
*
* */
String config = "conf/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
DicService dicService = (DicService) ac.getBean("dicService");
//System.out.println("上下文域对象创建");
System.out.println("服务器处理数据字典开始");
ServletContext application = servletContextEvent.getServletContext();
Map<String, List<DicValue>> map = dicService.getAll();
Set<String> set = map.keySet();
for (String key : set){
//存入全局作用域对象
application.setAttribute(key,map.get(key));
}
System.out.println("服务器缓存处理数据字典结束");
}
整合SSM框架中关于自定义Listener的小问题记录
最新推荐文章于 2022-04-01 09:42:55 发布