2017-04-23 32 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); 
    } 

}