如何在spring boot 2.0中找到Interface EmbeddedServletContainerCustomizer

时间:2017-04-23 13:01:36

标签: java spring-boot

我想在应用程序运行时更改绑定端口, 但遇到错误消息'EmbeddedServletContainerCustomizer无法解析为类型'。 我的Spring启动版本是2.0.0.BUILD-SNAPSHOT。

以下代码:

import org.springframework.boot.context.embedded.*;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
    container.setPort(9000);
}

}

非常感谢

3 个答案:

答案 0 :(得分:8)

就端口而言,我已经使用了已经回答的配置选项。

但是,您仍然可以使用自定义程序,但是,Spring Boot 2.0中的类型和位置会发生变化,请参阅:

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }

}

答案 1 :(得分:1)

SpringBoot可以轻松配置绑定端口,只需使用 server.port 在application.properties中设置自定义端口

答案 2 :(得分:-1)

使用SpringApplicationBuilder以编程方式设置属性server.port。在你的spring boot main方法中使用它。

HashMap<String, Object> properties = new HashMap<>();
properties.put("server.port", 9000);
new SpringApplicationBuilder()            
    .properties(properties)
    .run(args);