Springboot之修改啟動(dòng)端口的兩種方式(小結(jié))
Springboot啟動(dòng)的時(shí)候,端口的設(shè)定默認(rèn)是8080,這肯定是不行的,我們需要自己定義端口,Springboot提供了兩種方式,第一種,我們可以通過(guò)application.yml配置文件配置,第二種,可以通過(guò)代碼里面指定,在開(kāi)發(fā)中,建議使用修改application.yml的方式來(lái)修改端口。
代碼地址
#通過(guò)yml配置文件的方式指定端口地址https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-demo2#硬編碼的方式指定端口地址https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-demo3
修改application.yml配置文件改端口
這個(gè)地方,簡(jiǎn)單說(shuō)一下yml文件,其實(shí)這玩意和properties配置文件一樣,但是相對(duì)于properties文件更加簡(jiǎn)約一些 server.port=8888,在yml直接就變成下面的配置了,相同的頭就直接前面空三格子即可,這樣就將一些同類(lèi)型的配置放一塊了,比起properties,簡(jiǎn)單不少。
配置application.yml文件內(nèi)容
logging: #日志存儲(chǔ)地址 file: 'logs/config/demo-xx.log'info: name : '入門(mén)案例'server: #端口號(hào) port: 8888 #項(xiàng)目名,如果不設(shè)定,默認(rèn)是 / context-path: /demo
代碼指定端口
這種方式,是通過(guò)編碼的方式來(lái)硬性的指定了端口的配置
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) { //指定項(xiàng)目名稱(chēng) container.setContextPath('/demo'); //指定端口地址 container.setPort(8090); } }
訪問(wèn)結(jié)果
設(shè)置后,端口訪問(wèn)正常,但是總的來(lái)說(shuō),希望大家通過(guò)配置文件的方式來(lái)指定端口。
到此這篇關(guān)于Springboot之修改啟動(dòng)端口的兩種方式(小結(jié))的文章就介紹到這了,更多相關(guān)Springboot 修改啟動(dòng)端口內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. ASP實(shí)現(xiàn)加法驗(yàn)證碼3. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 詳解JSP 內(nèi)置對(duì)象request常見(jiàn)用法6. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算7. Python matplotlib 繪制雙Y軸曲線圖的示例代碼8. jsp EL表達(dá)式詳解9. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除10. springboot集成與使用Sentinel的方法
