摘要:比如日志默認(rèn)使用作為第一選擇,默認(rèn)集成了,并且支持配置使用貌似和有點(diǎn)變化,暫時(shí)不折騰了單元測(cè)試
環(huán)境:Spring Boot 1.5.4
基于 Spring Boot 創(chuàng)建一個(gè)命令行應(yīng)用,先來個(gè)最基本的體驗(yàn),體驗(yàn)一下:
配置管理(配置文件加載,多環(huán)境配置文件)
日志
單元測(cè)試
創(chuàng)建項(xiàng)目比較好的兩種方法:
通過 https://start.spring.io/ 網(wǎng)站,生成項(xiàng)目框架,導(dǎo)入到 IDE
IDEA 有Spring Boot的插件,直接按照提示創(chuàng)建
其他
創(chuàng)建個(gè)最基本的應(yīng)用,包含了devtools,logging,test,以及maven插件:
...配置管理 修改 banner... org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-devtools runtime org.springframework.boot spring-boot-starter-test test ... org.springframework.boot spring-boot-maven-plugin
Spring Boot 的默認(rèn) banner:
. ____ _ __ _ _ / / ___"_ __ _ _(_)_ __ __ _ ( ( )\___ | "_ | "_| | "_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) " |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE)
resources 目錄下創(chuàng)建一個(gè) banner.txt 文件可以修改,并且還提供了一些參數(shù),可以配色。
當(dāng)然也可以在配置文件或入口處關(guān)閉:
spring.main.banner-mode=off
或
public static void main(String[] args) { SpringApplication application = new SpringApplication(HelloApplication.class); application.setBannerMode(Banner.Mode.OFF); application.run(args); }
關(guān)閉 banner 竟然還弄這么多方式,我也是醉了,其實(shí)只是展示一下在入口處還可以進(jìn)行很多應(yīng)用的配置罷了。
自定義屬性如果不是特殊的應(yīng)用場(chǎng)景,就只需要在 application.properties 中完成一些屬性配置就能開啟各模塊的應(yīng)用。
application.properties:
mysql.host=default mysql.user=default_user mysql.mix=${mysql.host}/${mysql.user}
如上所示:參數(shù)之間也可以使用變量直接引用來使用
定義 MysqlProperties Class:
@Component public class MysqlProperties { @Value("${mysql.host:localhost}") private String host; @Value("${admin.user:root}") private String user; // 省略getter、setter、toString }
@Value 注解未免有點(diǎn)蛋疼
可以使用 @ConfigurationProperties 注解直接配置個(gè)屬性前綴,同時(shí)還可以加載一個(gè)額外的 .properties 文件
app.properties:
app.name=hello app.version=1.0
定義 AppProperties Class:
@Component @PropertySource("classpath:app.properties") @ConfigurationProperties(prefix = "app") public class AppProperties { private String name; private String version; // 省略getter、setter、toString }命令行運(yùn)行
Spring Boot 默認(rèn) Application 定義了 main 方法入口,所以要實(shí)現(xiàn)一個(gè)命令行運(yùn)行的應(yīng)用,需要實(shí)現(xiàn) CommandLineRunner 接口,覆寫 run 方法,這樣命令行參數(shù)就通過變長(zhǎng)參數(shù) strings 接受到。
@SpringBootApplication public class HelloApplication implements CommandLineRunner { @Override public void run(String... strings) throws Exception { } }多環(huán)境配置
Spring Boot中多環(huán)境配置文件名需要滿足application-{profile}.properties的格式,其中{profile}對(duì)應(yīng)你的環(huán)境標(biāo)識(shí),如:
application-dev.properties:開發(fā)環(huán)境 application-test.properties:測(cè)試環(huán)境
同時(shí),需要在application.properties文件中通過spring.profiles.active屬性來設(shè)置,其值對(duì)應(yīng){profile}值,并且可以設(shè)置多個(gè)。
其次,通過命令行參數(shù) --spring.profiles.active=test 可以切換多環(huán)境。比如:
java -jar xxx.jar --spring.profiles.active=test日志
Spring Boot 默認(rèn)使用 Logback 作為第一選擇,默認(rèn)集成了 slf4j,并且支持配置使用 Log4j:
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-log4j2
log4j2 貌似和 log4j 有點(diǎn)變化,暫時(shí)不折騰了
單元測(cè)試文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/67254.html
摘要:開始介紹簡(jiǎn)化了基于的應(yīng)用開發(fā),你只需要就能創(chuàng)建一個(gè)獨(dú)立的,產(chǎn)品級(jí)別的應(yīng)用。該包含很多搭建,快速運(yùn)行項(xiàng)目所需的依賴,并提供一致的,可管理傳遞性的依賴集。日志級(jí)別通過標(biāo)識(shí)開啟控制臺(tái)級(jí)別日志記錄,也可以在中指定日志級(jí)別配置示例 開始 介紹 Spring Boot 簡(jiǎn)化了基于 Spring 的應(yīng)用開發(fā),你只需要 run 就能創(chuàng)建一個(gè)獨(dú)立的,產(chǎn)品級(jí)別的 Spring 應(yīng)用。 Spring 平臺(tái)...
摘要:不過可以切換到版本,兼容性未知。注解一旦添加了依賴會(huì)判斷這是一個(gè)應(yīng)用,并啟動(dòng)一個(gè)內(nèi)嵌的容器默認(rèn)是用于處理請(qǐng)求。注意中空字符串與的區(qū)別。 環(huán)境:Spring Boot 1.5.4 基于 Spring Boot 可以快速創(chuàng)建一個(gè)Web & Restful 應(yīng)用,在開始應(yīng)用之前,至少要了解以下用法: 定義路由,定義 HTTP 方法 獲取Header、GET、POST、路徑等參數(shù) Cooki...
摘要:本文只是引子,后續(xù)更新到獨(dú)立章節(jié)。尤其是,這也是現(xiàn)在號(hào)稱流行的組合。幸虧現(xiàn)在看起來不主流了。增刪改查多條件組合查詢分頁,排序等多表關(guān)聯(lián)。而每個(gè)類寫上構(gòu)造函數(shù),,實(shí)在是蛋疼。 本文只是引子,后續(xù)更新到獨(dú)立章節(jié)。 環(huán)境:Spring Boot 1.5.4 到了操作數(shù)據(jù)庫的環(huán)節(jié),以 MySQL 為基準(zhǔn),體驗(yàn)一下數(shù)據(jù)庫的相關(guān)操作,先讓我糾結(jié)一下,至少有以下四種姿勢(shì)。 JDBC。原生的 JD...
摘要:響應(yīng)式編程是基于異步和事件驅(qū)動(dòng)的非阻塞程序,只是垂直通過在內(nèi)啟動(dòng)少量線程擴(kuò)展,而不是水平通過集群擴(kuò)展。三特性常用的生產(chǎn)的特性如下響應(yīng)式編程模型適用性內(nèi)嵌容器組件還有對(duì)日志消息測(cè)試及擴(kuò)展等支持。 摘要: 原創(chuàng)出處 https://www.bysocket.com 「公眾號(hào):泥瓦匠BYSocket 」歡迎關(guān)注和轉(zhuǎn)載,保留摘要,謝謝! 02:WebFlux 快速入門實(shí)踐 文章工程: JDK...
摘要:關(guān)聯(lián)關(guān)系的關(guān)聯(lián)關(guān)系定義上,感覺并不是很靈活,姿勢(shì)也比較難找。如,定義在關(guān)聯(lián)關(guān)系上的參數(shù)可以設(shè)置級(jí)聯(lián)的相關(guān)東西。因?yàn)樾蛄谢瘯?huì)涉及到實(shí)體類關(guān)聯(lián)對(duì)象的獲取,會(huì)觸發(fā)所有的關(guān)聯(lián)關(guān)系。 接(4) - Database 系列. Java Persistence API,可以理解就是 Java 一個(gè)持久化標(biāo)準(zhǔn)或規(guī)范,Spring Data JPA 是對(duì)它的實(shí)現(xiàn)。并且提供多個(gè) JPA 廠商適配,如 Hi...
閱讀 1057·2023-04-26 02:21
閱讀 2859·2021-09-24 09:47
閱讀 1651·2019-08-30 15:55
閱讀 2223·2019-08-30 14:01
閱讀 2383·2019-08-29 14:01
閱讀 2099·2019-08-29 12:46
閱讀 864·2019-08-26 13:27
閱讀 2005·2019-08-26 12:23