spring-boot: Why is my WebApplicationInitializer not loaded
I have a really simple Spring-Boot application and I wonder why my WebApplicationInitializer is not recognized by SpringServletContainerInitializer?
At application startup in console everythings looks fine, but there is no System.err-output from the Initializer. Also, if I set a breakpoint inside the onStartup method it is never reached.
Did I miss anything?
Thanks.
My classes:
package de.tbosch.web.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package de.tbosch.web.springboot;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
public class Initializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.err.println("------------------------------------");
}
}
Details: https://github.com/dickerpulli/playground/tree/master/web/spring-boot
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 15 (5 by maintainers)
Great hints …
Now I solved it just with using a ServletContextInitializer instead of WebApplicationInitializer and added this one to my spring context.
With declaring it as
@ConfigurationI can add it in the main-Method as follows… and it’s getting loaded.
Thanks a lot!
I have a Spring Boot project with a WebApplicationInitializer - the initializer works fine when I build a war and deploy it to a standalone Tomcat 7. However if I build my Spring Boot app as an executable jar with an embedded Tomcat 7, the WebApplicationInitialier is NOT called when the application is started.
For my project I need to deliver a war file, so it is not a big problem for me, but it would be nice to get this to work the same way regardless of using the embedded Tomcat or deploying to a standalone Tomcat.
I found an old issue on the Tomcat issue tracker that might be related to why my WebApplicationInitializer is not found - apparently the class has to be in a JAR file for the Tomcat scanner to find it. It should be possible to work around - by doing what Mark suggests in comment #19 - https://issues.apache.org/bugzilla/show_bug.cgi?id=52853#c19.
As for other ways to register servlets/filters in a Spring Boot Web application you should be able to create a @Bean in your configuration that returns a Servlet, or Filter - have a look at http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-add-a-servlet-filter-or-servletcontextlistener
Cheers Flemming
On 18 Mar 2014, at 20:53, Thomas Bosch notifications@github.com wrote:
As mentioned in #321 not calling
WebApplicationInitializersis an intentional design decision.You should be able to register a
@Beanof typeorg.springframework.boot.context.embedded.ServletContextInitializerand use that to delegate to your existingWebApplicationInitializer. Or you can registerServletsandFilters(either directly or using theServletRegistrationBeanorFilterRegistrationBeanclasses).I’m not sure what you were expecting, but Spring Boot apps do not execute
ServletContainerInitializersso this is not surprising (see #321 for more discussion). I think this is effectively a duplicate of #321 so I’ll close it if you don’t mind.What was it you acctually wanted to do? Maybe
ServletContextInitializerwas what you wanted? Or anApplicationListener?