一、通過配置
修改application.properties
在屬性文件中添加server.port=8000
二、直接看代碼:
1 @Controller 2 @EnableAutoConfiguration 3 @ComponentScan 4 public class CallMain extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer { 5 private final static Logger logger = LoggerFactory.getLogger(CallMain.class); 6 7 public static void main(String[] args) throws Exception { 8 9 //... 10 SpringApplication.run(CallMain.class, args); 11 logger.debug("spring boot system init successfully..."); 12 } 13 14 @Override 15 public void customize(ConfigurableEmbeddedServletContainer container) { 16 container.setPort(8000); 17 18 } 19 }
三、關鍵點
1. 啟動類要繼承SpringBootServletInitializer類。
2. 啟動類要實現EmbeddedServletContainerCustomizer接口。
3. 通過復寫customize方法實現端口的設置。
4. 這個接口在2.0.x版本中刪除了。