Why is my WebApplicationInitializer not loaded

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

17reactions
dickerpullicommented, Mar 19, 2014

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!

5reactions
frjtriforkcommented, Mar 18, 2014

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

spring - WebApplicationInitializer not loading - Stack Overflow
I am using tomcat 7. The problem remains there even if I remove hibernate dependency and update the project. What can be the...
Read more >
Spring WebApplicationInitializer - Java Development Journal
A quick and practical guide to Spring WebApplicationInitializer. ... One of the natural questions will arise in your mind is “If we are...
Read more >
Spring WebApplicationInitializer tutorial - ZetCode
Spring WebApplicationInitializer tutorial shows how to bootstrap Spring web applications programatically with WebApplicationInitializer.
Read more >
[Solved]-WebApplicationInitializer not reached-Spring MVC
Coding example for the question WebApplicationInitializer not reached-Spring MVC. ... your pom.xml file is missing the dependency:spring-context-support ...
Read more >
WebApplicationInitializer (Spring Framework 6.0.2 API)
The code-based approach with WebApplicationInitializer ... Even if a component (e.g. non-Spring, other third party) has not been specifically updated for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found