项目需要,增加监听,并且在监听器中增加数据库的操作,需要注入service。所以稍微上网搜索了下:
实现方式:
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sce
.getServletContext());
JobConfigServiceImpl configService = (JobConfigServiceImpl) context.getBean("configService");
//做configService的操作
}
注意服务名称必须在原service中注解,eg:@Service("configService")
且该listener必须在contextLoader之后加载