【问题标题】:Cannot configure port in TomcatConnectorCustomizer for Spring Boot无法在 TomcatConnectorCustomizer 中为 Spring Boot 配置端口
【发布时间】:2014-11-04 19:26:49
【问题描述】:

我有一个 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 应用程序,它在那里运行良好。如何进一步解决此问题?

【问题讨论】:

    标签: tomcat spring-boot


    【解决方案1】:

    您的外部属性中不能有server.settings.*(除非您采取一些步骤来排除它试图绑定的东西,或者将属性从Environment 中分离出来)。 Spring Boot 将 server.* 绑定到 ServerProperties 并且它没有“设置”属性。

    【讨论】:

    • 你是对的,我只是通过查看ServerProperties.java 的源代码才知道的。更好地使用 Spring 提供的属性。
    猜你喜欢
    • 2016-03-03
    • 1970-01-01
    • 2016-07-21
    • 2018-05-06
    • 2017-09-12
    • 2014-01-31
    相关资源
    最近更新 更多