2017-04-23 77 views
0

我想在应用程序运行时更改绑定端口, 但遇到错误消息“EmbeddedServletContainerCustomizer无法解析为某个类型”。 我的Spring启动版本是2.0.0.BUILD-SNAPSHOT。如何在弹簧引导2.0中找到接口EmbeddedServletContainerCustomizer

下面的代码:

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); 
} 

} 

非常感谢

+1

事情已经搬进2.0.0,以适应网络流量的支持。但是,如果你只想要一个不同的端口,只需使用'server.port = 9000'并放弃这个类。 –

回答

-1

使用SpringApplicationBuilder以编程方式设置属性server.port。在你的spring boot主要方法中使用这个。

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

非常感谢... – liuran

+0

如果我想设置随机端口可用,该怎么办? – vinesh

+0

只需使用server.port = 0(https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-user-a-random-unassigned-http-port) –

1

SpringBoot有绑定端口的配置简单,只需使用server.port设置在application.properties定制端口

+0

非常感谢,我的选择是使用SpringApplicationBuilder以编程方式设置端口。 – liuran

5

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

但是,你仍然可以使用一个定制,然而,类型和位置将在春季启动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); 
    } 

}