2014-11-04 71 views
2

我有一個Spring Boot應用程序,它將舊Web服務公開爲RESTful API。相關的代碼是:無法在TomcatConnectorCustomizer中爲Spring Boot配置端口

@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? 

我創建了一個示例春天啓動應用程序,它在那裏工作得很好。我如何進一步解決這個問題?

回答

5

您的外部屬性中不能有server.settings.*(除非您採取一些步驟排除它試圖綁定的東西,或者將屬性從Environment中分離出來)。 Spring Boot綁定server.*ServerProperties,它沒有「設置」屬性。

+0

你是對的,我只是通過查找['ServerProperties.java'](https://github.com/spring-projects/spring-boot/blob/v1.1.8.RELEASE /spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java)。更好地使用Spring提供的屬性。 – Phil 2014-11-04 22:36:13