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

posted @ 2018-06-22 15:28  耀仔  阅读(401)  评论(0编辑  收藏  举报