idea創(chuàng)建spring boot工程及配置文件(最新推薦)
一、dea 創(chuàng)建spring boot工程
new project
選擇Spring Initializr ,Type:選擇Maven,Java 8, Packagin 選擇Jar。然后點(diǎn)擊next
添加依賴:
選擇Sprint Boot版本,選不到自己想到的沒(méi)關(guān)系,后面我們?cè)趐om文件里修改版本號(hào)。
添加依賴,我們這里只添加Web---Spring Web ,然后點(diǎn)擊Finnish。會(huì)在pom文件里,加載這個(gè)依賴。
修改Spring Boot版本號(hào),我習(xí)慣用2.4.4 的,但是剛剛創(chuàng)建的是2.6.13 版本,修改pom文件
將2.6.13 修改成2.4.4。
然后重新加載依賴
二、打包
idea 右側(cè)Maven---Lifecycle--package。雙擊package
(clean用于清理target文件夾)
會(huì)在當(dāng)前項(xiàng)目?jī)?nèi)生成一個(gè)target文件夾,里面有jar包。
三、啟動(dòng)配置文件
application.properties 與application.yml 都可以。
springboot里內(nèi)置了tomcat,默認(rèn)端口是8080。
application.yml 配置端口
server: port: 8081 servlet: context-path: /boot
application.properties
server.port=8081 server.servlet.context-path=/boot
port指定端口號(hào),context-path,設(shè)置整個(gè)工程的uil,前邊需要添加/boot/xx
application.yml 使用的更普遍。
四、多環(huán)境配置
多環(huán)境配置,有多種方式,我們這里只講常用的。
新增application-dev.yml文件,與application-test.yml文件。
application-dev.yml文件,寫(xiě)我們dev環(huán)境的配置。(端口號(hào)、path等)
application-test.yml文件,寫(xiě)我們test環(huán)境的配置。(端口號(hào)、path等)
當(dāng)我們通過(guò)jar包運(yùn)行項(xiàng)目時(shí),想要運(yùn)行dev環(huán)境
java -jar xx.jar --spring.profiles.active=dev
想要運(yùn)行test環(huán)境
java -jar xx.jar --spring.profiles.active=test
五、讀取配置文件
5.1 讀取yaml配置文件
spring boot yaml自定義配置傳入代碼_做測(cè)試的喵醬的博客-CSDN博客
5.2 讀取properties配置文件
舉例,數(shù)據(jù)庫(kù)配置信息
1、新建數(shù)據(jù)庫(kù)配置類
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Data @Component @PropertySource("classpath:db.properties") @ConfigurationProperties(prefix = "mysql") public class MySqlCon { private String url; private String userName; private String password; }
注解@PropertySource("classpath:db.properties"),指定讀取配置的文件。我們?cè)谠趓esources文件夾中,數(shù)據(jù)庫(kù)的配置文件db.properties文件
使用@ConfigurationProperties(prefix = "mysql")注解,prefix = "mysql"設(shè)置的別名。在配置文件中,的key就是對(duì)應(yīng)的這個(gè)值。
2、在resources文件夾中,新建數(shù)據(jù)庫(kù)配置文件db.properties文件。
配置數(shù)據(jù)庫(kù)信息
mysql.url=jdbc:mysql://localhost:3306/bt mysql.userName=root mysql.password=123456
3、數(shù)據(jù)的引用
使用@Autowired注入就可以了。
@RestController public class HiController { @Autowired private Person person; @Autowired private MySqlCon mySqlCon;
到此這篇關(guān)于idea創(chuàng)建spring boot工程及配置的文章就介紹到這了,更多相關(guān)idea創(chuàng)建spring boot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于tomcat8 編寫(xiě)字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法
下面小編就為大家分享一篇基于tomcat8 編寫(xiě)字符編碼Filter過(guò)濾器無(wú)效問(wèn)題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01logback ThresholdFilter臨界值日志過(guò)濾器源碼解讀
這篇文章主要為大家介紹了logback ThresholdFilter臨界值日志過(guò)濾器源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11struts2簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Struts2框架是MVC流程框架,適合分層開(kāi)發(fā),這篇文章主要為大家詳細(xì)介紹了struts2簡(jiǎn)介的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09SpringMVC中的ResourceUrlProviderExposingInterceptor詳解
這篇文章主要介紹了SpringMVC中的ResourceUrlProviderExposingInterceptor詳解,ResourceUrlProviderExposingInterceptor是Spring MVC的一個(gè)HandlerInterceptor,用于向請(qǐng)求添加一個(gè)屬性,需要的朋友可以參考下2023-12-12Java利用棧實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Java利用棧實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05spring profile 多環(huán)境配置管理詳解
這篇文章主要介紹了 spring profile 多環(huán)境配置管理詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01