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

 mobiledu2502900255 发布于 2022-12-13 10:06

我有一个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中:


        
            
                
                org.springframework.boot
                spring-boot-starter-parent
                ${springboot-version}
                pom
                import
            
            
                org.springframework
                spring-framework-bom
                ${spring-version}
                pom
                import
            
        
    

    
        
            org.springframework
            spring-context
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    
    
        4.1.1.RELEASE
        1.1.8.RELEASE
    

它曾经工作正常,但现在我收到此错误:

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 个回答
  • 你不能拥有server.settings.*外部属性(除非你采取一些措施来排除它试图绑定的东西,或者将属性分离出来Environment).Spring Boot绑定server.*ServerProperties它并且没有"settings"属性.

    2022-12-13 10:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有