ServletContextListener中@Autowired失效的解决方法
@WebListener
public class ContextWebListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
LogHelper.info("web stop!");
System.out.println("dao++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
// SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext())
.getAutowireCapableBeanFactory().autowireBean(this);//加上这一句
LogHelper.info("web start!");
System.out.println("dao++++"+dao.count());
LogHelper.info("web start! finish");
}
@Autowired
private ApplicationMetricsDao dao;
}
原文地址:https://blog.csdn.net/flymu0808/article/details/50835064