@WebListener,@Component先后顺序为什么会变?
来源:10-19 扩展外部化配置属性源其他扩展点

deletejeff
2019-02-17
写了一个@WebListener,实现ServletContextListener接口;又写了一个@Component,实现ApplicationRunner接口;问题来了,通过springboot直接启动时,@WebListener里的contextInitialized方法先执行了,ApplicationRunner中的run方法后执行,但是!通过部署到外部tomcat容器启动时,执行顺序反过来了,求问这是什么原因?
写回答
1回答
-
这个是机制问题,在普通的 Spring Boot 场景中,@WebListener 是通过 @ServletComponentScan 初始化 ServletListenerRegistrationBean,在 Servlet 容器Bean 生命周期处理,所以先执行。而 Spring Boot 部署在 Servlet 容器时,WebApplicationInitializer
实现类 SpringBootServletInitializer 将限制性,所以 ApplicationRunner 先执行,而 ServletContextListener 后执行。10
相似问题