I need to start a spring boot project in an external Tomcat (no embedded). The application starts by defining, as per documentation, the necessary interfaces. The problem is that added listeners are never invoked (I'm referring to the contextInitialized method never called). I don't understand where the error is, I have tried several solutions to no avail:
@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
SpringApplicationBuilder applicationBuilder = builder.sources(TestApplication.class);
return applicationBuilder;
}
}
The TestWebConfig:
@Configuration
@EnableWebMvc
public class TestWebConfig implements WebMvcConfigurer {
}
Going to debug, the interface is called correctly:
public class TestWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(TestWebConfig.class);
ctx.setServletContext(servletContext);
servletContext.addListener(new ContextLoaderListener(ctx));
servletContext.addListener(new TestListener()); // <--- this is the problem
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(TestApplication .class);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
This is the listener that is not being invoked. I also tried with the @WebListener and @ServletComponentScan annotation but nothing. I have read that only the embedded Tomcat works.
public class TestListener implements ServletContextListener {
private static final Logger logger = LogManager.getLogger();
@Override
public void contextInitialized(ServletContextEvent ctx) {
logger.info("Hello");
}
@Override
public void contextDestroyed(ServletContextEvent arg0){
}
}
I also tried to declare the listener with the @Bean, as suggested in a post, still nothing or do an @Autowired in the WebApplicationInitializer to not pass it with new, still nothing.
Do you have any ideas?