Java 如何在SpringBoot2.0中找到接口EmbeddedServletContainerCustomizer

Java 如何在SpringBoot2.0中找到接口EmbeddedServletContainerCustomizer,java,spring-boot,Java,Spring Boot,我想在应用程序运行时更改绑定端口 但遇到错误消息“EmbeddedServletContainerCustomizer无法解析为类型”。 我的Spring启动版本是2.0.0.BUILD-SNAPSHOT 以下代码: import org.springframework.boot.context.embedded.*; import org.springframework.stereotype.Component; @Component public class CustomizationBe

我想在应用程序运行时更改绑定端口 但遇到错误消息“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);
}

}

非常感谢

SpringBoot有一个简单的绑定端口配置,只需使用server.port在application.properties中设置自定义端口

使用SpringApplicationBuilder以编程方式设置属性server.port。在spring boot主方法中使用此选项

HashMap<String, Object> properties = new HashMap<>();
properties.put("server.port", 9000);
new SpringApplicationBuilder()            
    .properties(properties)
    .run(args);
HashMap properties=newhashmap();
properties.put(“server.port”,9000);
新的SpringApplicationBuilder()
.物业(物业)
.run(args);

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

但是,您仍然可以使用自定义程序,但是,类型和位置将在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);
    }

}
导入org.springframework.boot.web.server.WebServerFactoryCustomizer;
导入org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
导入org.springframework.stereotype.Component;
@组成部分
公共类CustomizationBean实现WebServerFactoryCustomizer{
@凌驾
public void自定义(ConfigurableServletWebServerFactory服务器){
服务器设置端口(9000);
}
}

为了适应对web流量的支持,2.0.0版中的内容有所改变。但是,如果您只需要一个不同的端口,请使用服务器.port=9000并放弃该类。非常感谢,我的选择是使用SpringApplicationBuilder以编程方式设置端口。如果我想设置可用的随机端口,该怎么办?只需使用服务器.port=0()