由于好久没有用过spring 最近写一个自定义监听器 中的service 属性注入问题 一直注入不进去值 原来是ServletContextListener 跟spring 一起启动的 注入不进去
解决的办法是
加 private WebApplicationContext springContext;
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
if(springContext != null){
ordersService = (OrdersService)springContext.getBean("ordersService");
subscriberService = (SubscriberService)springContext.getBean("subscriberService");
carGroupService = (CarGroupService)springContext.getBean("carGroupService");
}else{
System.out.println("获取应用程序上下文失败!");
return;
}
System.out.println("初始化系统服务!");
Th1 th1 = new Th1();
th1.start();
}
写下来免得以后再忘记