Springboot之修改啟動端口的兩種方式(小結)
Springboot啟動的時候,端口的設定默認是8080,這肯定是不行的,我們需要自己定義端口,Springboot提供了兩種方式,第一種,我們可以通過application.yml配置文件配置,第二種,可以通過代碼里面指定,在開發中,建議使用修改application.yml的方式來修改端口。
代碼地址
#通過yml配置文件的方式指定端口地址https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-demo2#硬編碼的方式指定端口地址https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-demo3
修改application.yml配置文件改端口
這個地方,簡單說一下yml文件,其實這玩意和properties配置文件一樣,但是相對于properties文件更加簡約一些 server.port=8888,在yml直接就變成下面的配置了,相同的頭就直接前面空三格子即可,這樣就將一些同類型的配置放一塊了,比起properties,簡單不少。
配置application.yml文件內容
logging: #日志存儲地址 file: 'logs/config/demo-xx.log'info: name : '入門案例'server: #端口號 port: 8888 #項目名,如果不設定,默認是 / context-path: /demo
代碼指定端口
這種方式,是通過編碼的方式來硬性的指定了端口的配置
package com.yellowcong.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;import org.springframework.boot.web.support.SpringBootServletInitializer;@SpringBootApplicationpublic class ConfigMain extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(ConfigMain.class); } public static void main(String[] args) { SpringApplication.run(ConfigMain.class, args); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { //指定項目名稱 container.setContextPath('/demo'); //指定端口地址 container.setPort(8090); } }
訪問結果
設置后,端口訪問正常,但是總的來說,希望大家通過配置文件的方式來指定端口。
到此這篇關于Springboot之修改啟動端口的兩種方式(小結)的文章就介紹到這了,更多相關Springboot 修改啟動端口內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: