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)

Commits related to this issue

Most upvoted comments

Great hints …

Now I solved it just with using a ServletContextInitializer instead of WebApplicationInitializer and added this one to my spring context.

@Configuration
public class Initializer implements ServletContextInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.err.println("------------------------------------");
    }

}

With declaring it as @Configuration I can add it in the main-Method as follows

    public static void main(String[] args) {
        SpringApplication.run(new Class[] { Application.class, Initializer.class }, args);
    }

… 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:

I want to register a servlet (CXFServlet) to the servlet context. With deployment in tomcat servlet-3.0 container it works with such a WebApplicationInitializer that registers the servlet in the context.

What do I have to do to make it work with spring boot?

— Reply to this email directly or view it on GitHub.

As mentioned in #321 not calling WebApplicationInitializers is an intentional design decision.

You should be able to register a @Bean of type org.springframework.boot.context.embedded.ServletContextInitializer and use that to delegate to your existing WebApplicationInitializer. Or you can register Servlets and Filters (either directly or using the ServletRegistrationBean or FilterRegistrationBean classes).

I’m not sure what you were expecting, but Spring Boot apps do not execute ServletContainerInitializers so 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 ServletContextInitializer was what you wanted? Or an ApplicationListener?