本人在以war包形式部署springboot项目的时候踩到的一个坑
问题说明:
springboot项目在开发环境启动时是毫无问题的,然而一打war包,tomcat控制台一直报错,监听类中的service始终是null,也就是说@autowise注解失效了。
通过查找资料,解决方法如下
@WebListener public class InitDataListener implements ServletContextListener { @Autowired private TechnologysService technologysService; @Autowired private TechnologysTypeService technologysTypeService; @Autowired private LinkService linkService; @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("-------------------------初始化数据-------------------------"); ServletContext servletContext = sce.getServletContext(); // 打war包的时候是无法将service给注入进来的 WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()) .getAutowireCapableBeanFactory().autowireBean(this); // 技术类别列表(导航条) servletContext.setAttribute("technologysTypeList", technologysTypeService.listPager(null,null)); }
启动类需要加的注解也一并copy了
@EnableTransactionManagement @MapperScan(value = "com.javaxl.p4.mapper") @ServletComponentScan(value= "com.javaxl.p4.listener") @SpringBootApplication public class P4Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(P4Application.class); } public static void main(String[] args) { SpringApplication.run(P4Application.class, args); } }
然后打war包,这个就上篇博客有所介绍了,不再多说废话了。最终含有监听类的springboot项目就可以发布成功了。
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有