FatalBeanException:EmbeddedServletContainerFactory实现无法实例化

时间:2018-01-10 10:08:16

标签: java spring tomcat spring-boot ssl-certificate

我有以下配置:

@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;
    }

在属性文件中我有以下几行:

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

当我开始申请时,我看到以下错误:

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)

我不明白这个问题的根本原因。请解释我如何解决这个问题

P.S。

我注意到如果替换

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);
            }
        };

TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() - 应用已成功启动

3 个答案:

答案 0 :(得分:2)

可能是由于Spring代理机制,它要求TomcatEmbeddedServletContainerFactory是适当的类。

实际上,您只需要按照错误消息中的说明进行操作,并将Factory正确地子类化为静态内部类或独立类即可。

答案 1 :(得分:0)

management.port=8090
server.port=8090

这两个必须相同。

答案 2 :(得分:-1)

我不明白为什么但是在我删除之后

management.port=8090

申请开始运作