Springboot ServletContextListener @Autowired null 解决办法
来源:互联网 发布:python股票自动交易 编辑:程序博客网 时间:2024/05/21 19:22
是用SpringBoot框架进行开发的时候,如果使用ServletContextListener对spring启动前和停止前做一些清理工作的时时候,需要用到一些autowired的类,测试发现这些类在启动的时候,由于相应的beanfactory还没有加载,所以会出现问题。找了一段时间,发现下面的方法能够有效解决这个问题。解决办法也是参照stackflow上面的。。
@WebListenerpublic class ContextWebListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stubLogHelper.info("web stop!");System.out.println("dao++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");}@Overridepublic 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");}@Autowiredprivate ApplicationMetricsDao dao;}
按照上面的做法,就可以使用autowired了。
1 0
- Springboot ServletContextListener @Autowired null 解决办法
- springboot拦截器@Autowired为null解决
- Springboot ServletContextListener 注入失败解决方案
- HandlerInterceptor @Autowired报 null
- @Autowired 对象为null
- spring @Autowired为null
- SpringBoot@Autowired不自动注入
- 解决@Autowired在ServletContextListener中失效的一种方法
- @Autowired注解getBean为null
- ServletContextListener
- ServletContextListener
- ServletContextListener
- ServletContextListener
- ServletContextListener
- ServletContextListener
- ServletContextListener
- ServletContextListener
- ServletContextListener
- PopupWindow的使用和分析 弹出框的一种
- 对象引用与对象的区别
- Vim基础知识
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- Activity 按返回键 重复返回同一Activity 解决方法
- Springboot ServletContextListener @Autowired null 解决办法
- char 与 unsigned char的本质区别
- Dedecms网站搬家
- Android Studio selector 报 Element selector must be declared 的问题
- Linux平台下文件统计
- 1002-切面条
- 【CSS-02】em和span的区别
- springMVC中发送邮件
- android取图片视频的缩略图