无法在TomcatConnectorCustomizer中为Spring Boot配置端口

时间:2014-11-04 19:26:50

标签: tomcat spring-boot

我有一个Spring Boot应用程序,它将遗留Web服务公开为RESTful API。相关代码将是:

@Configuration
@PropertySource("classpath:foo.properties")
@ComponentScan("foo.bar")
@EnableAutoConfiguration
public class Application {

    @Autowired
    private Environment env;

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer()  throws FileNotFoundException {
        final String absoluteKeystoreFile = ResourceUtils.getFile(env.getProperty("security.settings.keystore.path")).getAbsolutePath();
        System.out.println("PATH: " + absoluteKeystoreFile);

        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(  ConfigurableEmbeddedServletContainer factory) {
                if (factory instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory;
                    containerFactory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                        @Override
                        public void customize(Connector connector) {
                            connector.setPort(Integer.parseInt(env.getProperty("server.settings.port")));
                            connector.setDomain(env.getProperty("server.settings.address"));
                            connector.setSecure(true);
                            connector.setScheme("https");
                            Http11NioProtocol proto = (Http11NioProtocol) connector.getProtocolHandler();
                            proto.setSSLEnabled(true);
                            proto.setKeystoreFile(absoluteKeystoreFile);
                            proto.setKeystorePass(env.getProperty("security.settings.keystore.pass"));
                            proto.setKeystoreType(env.getProperty("security.settings.keystore.type"));
                            proto.setKeyAlias(env.getProperty("security.settings.key.alias"));
                        }
                    });
                }
            }
        };
    }

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

在我的POM中:

<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${springboot-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <spring-version>4.1.1.RELEASE</spring-version>
        <springboot-version>1.1.8.RELEASE</springboot-version>
    </properties>

过去工作正常,但现在我收到了这个错误:

2014-11-04 12:18:10.638  WARN 664 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.web.ServerProperties org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration.properties; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverProperties': Could not bind properties; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'settings[port]' of bean class [org.springframework.boot.autoconfigure.web.ServerProperties]: Cannot access indexed value in property referenced in indexed property path 'settings[port]'; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'settings[port]' of bean class [org.springframework.boot.autoconfigure.web.ServerProperties]: Bean property 'settings[port]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

我创建了一个示例Spring Boot应用程序,它在那里工作正常。我该如何进一步解决这个问题?

1 个答案:

答案 0 :(得分:5)

您的外部属性中不能包含server.settings.*(除非您采取一些措施来排除它尝试绑定的内容,或者将属性从Environment中分离出来)。 Spring Boot将server.*绑定到ServerProperties,并且没有“settings”属性。