gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:55:29 27 4
gpt4 key购买 nike

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

最佳答案

你不能在你的外部属性中有 server.settings.*(除非你采取一些步骤来排除它试图绑定(bind)的东西,或者将属性从 环境)。 Spring Boot 将 server.* 绑定(bind)到 ServerProperties 并且它没有“settings”属性。

关于tomcat - 无法在 TomcatConnectorCustomizer 中为 Spring Boot 配置端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com