I'm not sure if it's a Spring Boot 1.2.0 or Undertow issue.

However: 1. Spring should report the problematic bean name/path, including the @Configuration where it's located 2. How do I resolve this... can I add servlet context listeners? 3. I should report the typo programatically to Undertow project

13:53:47.102 WARN  |            |                                                                                                    | main             | figEmbeddedWebApplicationContext | Exception encountered during context initialization - cancelling refresh attempt
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.RuntimeException: java.lang.IllegalArgumentException: UT010043: Cannot add servlet context listener from a programatically added listener
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:124) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:474) [spring-context-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java) [spring-context-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java) [spring-context-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at id.co.bippo.springapp.CommerceServerApplication.main(CommerceServerApplication.java:66) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) [idea_rt.jar:na]
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: UT010043: Cannot add servlet context listener from a programatically added listener
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:222) ~[undertow-servlet-1.1.1.Final.jar:1.1.1.Final]
    at org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory.createDeploymentManager(UndertowEmbeddedServletContainerFactory.java:345) ~[spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory.getEmbeddedServletContainer(UndertowEmbeddedServletContainerFactory.java:201) ~[spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:148) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:121) [spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    ... 13 common frames omitted
Caused by: java.lang.IllegalArgumentException: UT010043: Cannot add servlet context listener from a programatically added listener
    at io.undertow.servlet.spec.ServletContextImpl.addListener(ServletContextImpl.java:606) ~[undertow-servlet-1.1.1.Final.jar:1.1.1.Final]
    at org.springframework.boot.context.embedded.ServletListenerRegistrationBean.onStartup(ServletListenerRegistrationBean.java:108) ~[spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:203) ~[spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory$StartupListener.contextInitialized(UndertowEmbeddedServletContainerFactory.java:508) ~[spring-boot-1.2.0.RELEASE.jar:1.2.0.RELEASE]
    at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173) ~[undertow-servlet-1.1.1.Final.jar:1.1.1.Final]
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:193) ~[undertow-servlet-1.1.1.Final.jar:1.1.1.Final]
    ... 17 common frames omitted

Comment From: ceefour

Only happens with Undertow. After I replaced to Tomcat 8 embedded this error no longer appears.

Comment From: wilkinsona

I've updated the Undertow sample with a ServletContextInitializer bean:

    @Bean
    public ServletContextInitializer myInitializer() {
        return new ServletContextInitializer() {

            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                System.out.println("Startup of " + servletContext);
            }
        };

    }

The initialiser's called as expected during startup. You must be doing something differently. Can you please describe the steps to reproduce the problem you're seeing.

Comment From: ceefour

ServletContextInitializer? I don't think I'm using it in my project.

The only clue of my class in the stacktrace is:

    at id.co.bippo.springapp.CommerceServerApplication.main(CommerceServerApplication.java:66) [classes/:na]

which isn't very helpful :(

Comment From: wilkinsona

You must be doing something to trigger the problem. We have a couple of Undertow samples that both work fine. Perhaps you could share a project that reproduces the problem or at least describe what you're doing

Comment From: wilkinsona

Nevermind, I've reproduced the problem now.

Comment From: ceefour

Wow! Awesome :)

My project is already very complex so it'd take some time to find the cause... It's a WAR project semi-converted to Spring Boot 1.1.19 recently upgraded to Spring Boot 1.2.0 with Tomcat 8 that's why I'm trying to make it work with Undertow. (which would be awesome, but in any case it's working now with Tomcat 8 since the upgrade from Tomcat 7 was flawless)

Thanks for finding it :)

Anyway if you can put an additional catch on such exceptions with more detailed info that'd help in the future when any further problem occur.

Comment From: wilkinsona

This is the same problem as #2058.

The root cause is that we're attempting to call ServletContext.addListener() with a ServletContextListener from within a call to ServletContextInitializer.contextInitialized().

The javadoc for addListener states that:

The given listenerClass must implement one or more of the following interfaces: - ServletContextAttributeListener - ServletRequestListener - ServletRequestAttributeListener - HttpSessionListener - HttpSessionAttributeListener

Note that ServletContextListener is not in this list. It goes on to say:

If this ServletContext was passed to ServletContainerInitializer.onStartup, then the given listenerClass may also implement ServletContextListener, in addition to the interfaces listed above.

So we can programmatically register ServletContextListeners but, according to the spec, we're only allowed to do so from a ServletContainerInitializer and not from a ServletContextInitializer as we're currently doing.

We use a LifecycleListener with Tomcat to, I think, avoid this problem. Jetty 8 doesn't appear to enforce this restriction. Jetty 9 does, but it can be relaxed by calling setExtendedListenerTypes(true). We need to do something similar for Undertow.

Comment From: wilkinsona

Undertow can be fixed by using a ServletContainerInitializer to drive all of the ServletContextInitializers.

I'm worried about the inconsistency across the three containers. I wonder if we shouldn't change all three embedded containers to use a ServletContainerInitializer to call all the ServletContextInitializers.

@philwebb @dsyer What do you think?

Comment From: wilkinsona

I've opened #2197 to improve the diagnostics when ServletContext.addListener fails.

Comment From: philwebb

A nervous +1 for the switch. I'd rather not change at a point release, but I also don't like violating the spec.

Have you tried it yet?

Comment From: wilkinsona

I tried it for Undertow and it worked. I'll try it for Jetty and Tomcat and report back.

Comment From: wilkinsona

I think I have a fix that works across all three containers. Some of the class names certainly need some love and the changes in general should probably be reviewed given the risk... See 5b02aec.

Comment From: philwebb

I've made a few edits and dropped the ServletContextInitializerDrivingServletContainerInitializer. I'd ideally like to remove the required dependency on jetty-plus before merging. See https://github.com/philwebb/spring-boot/commit/bd354b2747a512177f5b147462772821de49ef89

Comment From: wilkinsona

Thanks, Phil. I can't see how we can easily use a ServletContainerInitializer with Jetty without jetty-plus. It's only ~50KB so I didn't think it was too bad. I guess we could duplicate the logic from ContainerInitializer. It temporarily enables extended listeners and sets up the TCCL IIRC.

Comment From: philwebb

I'm not worried about the size, but I am worried that someone might be using Jetty without the starter and get a ClassNotFoundException.

Comment From: ceefour

Awesome !! :-)

On Tue, Dec 23, 2014, 02:27 Andy Wilkinson notifications@github.com wrote:

Closed #2192 https://github.com/spring-projects/spring-boot/issues/2192 via e57b0e0 https://github.com/spring-projects/spring-boot/commit/e57b0e00f24a4e0d5ac3120f56aaa1991064b48f .

— Reply to this email directly or view it on GitHub https://github.com/spring-projects/spring-boot/issues/2192#event-211054459 .