gstackoverflow
gstackoverflow

Reputation: 36984

FatalBeanException: EmbeddedServletContainerFactory implementation cannot be instantiated

I have following configuration:

@Configuration
public class ConnectorConfig {
    @Value("${server.port}")
    private Integer port;

    @Bean
    public TomcatEmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(getHttpConnector());
        tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            // configure maxSwallowSize
            if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
                // -1 means unlimited, accept bytes
                ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }
        });
        return tomcat;
    }

and in property file I have following lines:

server.address=0.0.0.0
server.port=8080
management.address=0.0.0.0
management.port=8090

When I start application I see following error:

10.01.18 13:01:01.570 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.FatalBeanException: EmbeddedServletContainerFactory implementation packacge.configuration.ConnectorConfig$1 cannot be instantiated. To allow a separate management port to be used, a top-level class or static inner class should be used instead
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration.determineEmbeddedServletContainerFactoryClass(EndpointWebMvcAutoConfiguration.java:219)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration.registerEmbeddedServletContainerFactory(EndpointWebMvcAutoConfiguration.java:205)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration.createChildManagementContext(EndpointWebMvcAutoConfiguration.java:190)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration.afterSingletonsInstantiated(EndpointWebMvcAutoConfiguration.java:156)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.debeers.mis.Application.main(Application.java:12)

I don't understand the root cause of this problem. Please explain me how to fix this issue

P.S.

I noticed if replace

TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };

with TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() - app starts successfully

Upvotes: 0

Views: 1140

Answers (3)

kupaff
kupaff

Reputation: 329

management.port=8090
server.port=8090

these two have to be the same.

Upvotes: 0

real_paul
real_paul

Reputation: 564

Its probably due to Spring proxying mechanism that it requires the TomcatEmbeddedServletContainerFactory to be a proper class.

Indeed you simply have to follow the instructions in the error message and properly subclass the Factory either as static inner class or a standalone class.

Upvotes: 3

gstackoverflow
gstackoverflow

Reputation: 36984

I don't understand why but after I removed

management.port=8090

application became working

Upvotes: -1

Related Questions