1. 配置文件簡(jiǎn)介
spring boot使用一個(gè)全局配置文件:application.properties或者application.yml,放置在src/main/resources目錄下或者類路徑的/config目錄下。
application.properties是我們熟知的鍵值對(duì)配置文件:
application.yml是yaml語言的配置文件溉瓶,yaml是一種以數(shù)據(jù)為中心的語言较雕,在配置數(shù)據(jù)的時(shí)候具有面向?qū)ο蟮奶卣髑傲颉N覀円院蟮拇a都會(huì)使用yml格式的配置文件芹敌。:
spring boot 的全局配置文件的作用是對(duì)一些默認(rèn)配置的配置進(jìn)行修改。后面我們會(huì)詳細(xì)的講解伸头。
2. pom配置文件(starter)
pom,是maven的配置文件舷蟀,gradel也差不多恤磷,因?yàn)槲乙恢痹谟胢aven弧轧,所以就講maven了。
為了解決大型項(xiàng)目以及一些常用組件jar包過多的問題碗殷,spring boot 為我們提供了許多starter pom精绎,每一個(gè)pom都已經(jīng)添加好了對(duì)應(yīng)的依賴,以往我們需要寫茫茫多的dependence锌妻,但是現(xiàn)在只需要一個(gè)starter就可以了代乃,并且spring boot 還為它們提供了默認(rèn)的配置和自動(dòng)配置的bean。
2.1 官方starter
- 2.1.1 應(yīng)用程序的starters
名稱 | 描述 |
---|---|
spring-boot-starter | 核心Spring Boot starter仿粹,包括自動(dòng)配置支持搁吓,日志和YAML |
spring-boot-starter-actuator | 生產(chǎn)準(zhǔn)備的特性,用于幫我們監(jiān)控和管理應(yīng)用 |
spring-boot-starter-amqp | 對(duì)”高級(jí)消息隊(duì)列協(xié)議”的支持吭历,通過spring-rabbit實(shí)現(xiàn) |
spring-boot-starter-aop | 對(duì)面向切面編程的支持堕仔,包括spring-aop和AspectJ |
spring-boot-starter-batch | 對(duì)Spring Batch的支持,包括HSQLDB數(shù)據(jù)庫 |
spring-boot-starter-cloud-connectors | 對(duì)Spring Cloud Connectors的支持晌区,簡(jiǎn)化在云平臺(tái)下(例如摩骨,Cloud Foundry 和Heroku)服務(wù)的連接 |
spring-boot-starter-data-elasticsearch | 對(duì)Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch |
spring-boot-starter-data-gemfire | 對(duì)GemFire分布式數(shù)據(jù)存儲(chǔ)的支持朗若,包括spring-data-gemfire |
spring-boot-starter-data-jpa | 對(duì)”Java持久化API”的支持恼五,包括spring-data-jpa,spring-orm和Hibernate |
spring-boot-starter-data-mongodb | 對(duì)MongoDB NOSQL數(shù)據(jù)庫的支持哭懈,包括spring-data-mongodb |
spring-boot-starter-data-rest | 對(duì)通過REST暴露Spring Data倉庫的支持灾馒,通過spring-data-rest-webmvc實(shí)現(xiàn) |
spring-boot-starter-data-solr | 對(duì)Apache Solr搜索平臺(tái)的支持,包括spring-data-solr |
spring-boot-starter-freemarker | 對(duì)FreeMarker模板引擎的支持 |
spring-boot-starter-groovy-templates | 對(duì)Groovy模板引擎的支持 |
spring-boot-starter-hateoas | 對(duì)基于HATEOAS的RESTful服務(wù)的支持遣总,通過spring-hateoas實(shí)現(xiàn) |
spring-boot-starter-hornetq | 對(duì)”Java消息服務(wù)API”的支持睬罗,通過HornetQ實(shí)現(xiàn) |
spring-boot-starter-integration | 對(duì)普通spring-integration模塊的支持 |
spring-boot-starter-jdbc | 對(duì)JDBC數(shù)據(jù)庫的支持 |
spring-boot-starter-jersey | 對(duì)Jersey RESTful Web服務(wù)框架的支持 |
spring-boot-starter-jta-atomikos | 對(duì)JTA分布式事務(wù)的支持,通過Atomikos實(shí)現(xiàn) |
spring-boot-starter-jta-bitronix | 對(duì)JTA分布式事務(wù)的支持旭斥,通過Bitronix實(shí)現(xiàn) |
spring-boot-starter-mail | 對(duì)javax.mail的支持 |
spring-boot-starter-mobile | 對(duì)spring-mobile的支持 |
spring-boot-starter-mustache | 對(duì)Mustache模板引擎的支持 |
spring-boot-starter-redis | 對(duì)REDIS鍵值數(shù)據(jù)存儲(chǔ)的支持容达,包括spring-redis |
spring-boot-starter-security | 對(duì)spring-security的支持 |
spring-boot-starter-social-facebook | 對(duì)spring-social-facebook的支持 |
spring-boot-starter-social-linkedin | 對(duì)spring-social-linkedin的支持 |
spring-boot-starter-social-twitter | 對(duì)spring-social-twitter的支持 |
spring-boot-starter-test | 對(duì)常用測(cè)試依賴的支持,包括JUnit, Hamcrest和Mockito琉预,還有spring-test模塊 |
spring-boot-starter-thymeleaf | 對(duì)Thymeleaf模板引擎的支持董饰,包括和Spring的集成 |
spring-boot-starter-velocity | 對(duì)Velocity模板引擎的支持 |
spring-boot-starter-web | 對(duì)全棧web開發(fā)的支持, 包括Tomcat和spring-webmvc |
spring-boot-starter-websocket | 對(duì)WebSocket開發(fā)的支持 |
spring-boot-starter-ws | 對(duì)Spring Web服務(wù)的支持 |
- 2.1.2 spring boot生產(chǎn)準(zhǔn)備的starters:
名稱 | 描述 |
---|---|
spring-boot-starter-actuator | 添加生產(chǎn)準(zhǔn)備特性圆米,比如指標(biāo)和監(jiān)控 |
spring-boot-starter-remote-shell | 添加遠(yuǎn)程ssh shell支持 |
- 2.1.3 排除或交換具體技術(shù)方面的starters
名稱 | 描述 |
---|---|
spring-boot-starter-jetty | 導(dǎo)入Jetty HTTP引擎(作為Tomcat的替代) |
spring-boot-starter-log4j | 對(duì)Log4J日志系統(tǒng)的支持 |
spring-boot-starter-logging | 導(dǎo)入Spring Boot的默認(rèn)日志系統(tǒng) |
spring-boot-starter-tomcat | 導(dǎo)入Spring Boot的默認(rèn)HTTP引擎 |
spring-boot-starter-undertow | 導(dǎo)入U(xiǎn)ndertow HTTP引擎(作為Tomcat的替代) |
2.2 非官方starter
也有一些第三方為spring boot 提供了starter:
- Handlebars
- Vaadin
- Apache Camel
- WRO4J
- Spring Batch(高級(jí)用法)
- HDIV
- Jade Templates(Jade4J)
- Actitivi
這些都可以在github上找到卒暂,感興趣的可以去搜一下。
3. xml配置文件
spring boot 提倡零配置娄帖,也就是無xml配置也祠,但是在實(shí)際項(xiàng)目中,可能有一些要求必須使用xml配置近速,這時(shí)我們可以使用spring提供的@ImportResource 來加載xml配置诈嘿,比如:
@ImportResource({"classpath:some-context.xml","classpath:other-context.xml"})
4. 命令行參數(shù)配置
上一篇文章中也有提到堪旧,spring boot 項(xiàng)目是可以打成jar包的,然后通過java -jar xxx.jar
來執(zhí)行奖亚,我們都知道淳梦,main函數(shù)是可以接受參數(shù)的,同樣昔字,spring boot的main函數(shù)也可以爆袍。
我們可以通過下面的命令來修改Tomcat的端口號(hào):
java -jar xxx.jar --server.port=9090
5. 自定義屬性配置
5.1 普通配置
在寫spring項(xiàng)目的時(shí)候,我們可以在properties文件中定義一個(gè)變量作郭,然后再代碼中通過 @PropertySource 指明 properties 文件的位置陨囊,然后通過@Value注入相應(yīng)的值。
在spring boot 中我們同樣可以自定義一個(gè)變量夹攒,因?yàn)閟pring boot中的全局配置文件默認(rèn)在一個(gè)目錄下蜘醋,所以我們可以直接用@Value注入值。
比如咏尝,在上一篇文章的demo項(xiàng)目中压语,我們可以在application.yml文件中這樣定義:
key:
hello: hello world
修改DemoApplication.java代碼為:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@RestController
public class DemoApplication {
@Value("${key.hello}")
private String hello;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/hello")
public String hello(){
return hello;
}
}
運(yùn)行項(xiàng)目,在瀏覽器中訪問http://localhost:8080/hello:
值被正常的注入進(jìn)去了状土。
5.2 類型安全的配置
上面的例子中无蜂,使用@Value注入每個(gè)配置在實(shí)際的應(yīng)用中會(huì)顯得格外的麻煩,因?yàn)榕渲靡话銜?huì)有很多蒙谓,用上面的方式就要寫好多@Value,并且如果在多個(gè)類文件中都使用這個(gè)值的話训桶,工作量會(huì)更大累驮。所以,需要一種更好的方式舵揭。并且谤专,這種方式是有安全隱患的,比如我需要的是一個(gè)long類型午绳,但是注入的卻是String置侍,這個(gè)時(shí)候就報(bào)錯(cuò)了:
Failed to convert value of type 'java.lang.String' to required type 'long';
spring boot 提供了基于類型安全的配置方式,通過@ConfigurationProperties將properties屬性和一個(gè)bean及其屬性關(guān)聯(lián)拦焚,從而實(shí)現(xiàn)類型安全的配置蜡坊。
我們來看一下:
application.yml:
key:
name: cleverfan
sex: man
在demo項(xiàng)目中創(chuàng)建一個(gè)class: Person.java
@Component
@ConfigurationProperties(prefix = "key")
public class Person {
private String name;
private String sex;
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
}
修改DemoApplication.java :
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@RestController
public class DemoApplication {
@Autowired
private Person person;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/hello")
public String hello(){
return person.getName() + ": " + person.getSex();
}
}
啟動(dòng)項(xiàng)目,訪問http://localhost:8080/hello:
回到剛剛的問題赎败,如果這個(gè)時(shí)候我需要的是long秕衙,但是給的是String怎么辦呢?這里給出一個(gè)很簡(jiǎn)單的處理方式:
Person.java
@Component
@ConfigurationProperties(prefix = "key")
public class Person {
private String name;
private long sex;
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
try{
this.sex = Long.valueOf(sex);
}catch (Exception e){
this.sex = 0L;
}
}
public String getName() {
return name;
}
public long getSex() {
return sex;
}
}
yml配置文件不變僵刮,我們傳遞的是字符串据忘,這次重啟項(xiàng)目鹦牛,訪問http://localhost:8080/hello:
這樣就可以簡(jiǎn)單的保證數(shù)據(jù)的安全了。
6. 日志配置
spring boot 支持Java Util Loggin勇吊、Log4J曼追、Log4J2和LogBack作為日志框架,無論使用哪種日志框架汉规,spring boot已為當(dāng)前使用日志框架的控制臺(tái)輸出及文件輸出做好了配置拉鹃。
默認(rèn)情況下,spring boot 使用 Logback作為日志框架鲫忍「嘌啵可以在配置文件中(yml)修改默認(rèn)配置文件的配置:
- 配置日志級(jí)別:
logging:
level: debug
- 配置日志文件
logging:
org:
springframework:
web: debug
7. Profile配置
Profile 是 spring 用來針對(duì)不同的環(huán)境對(duì)不同的配置提供支持的,比如生成環(huán)境悟民,測(cè)試環(huán)境和開發(fā)環(huán)境坝辫。全局 Profile 使用application-{profile}.yml(例如application-prod.yml)命名。
下面我們做一個(gè)演示射亏,我們把demo項(xiàng)目分為生產(chǎn)(prod)和開發(fā)(dev)環(huán)境近忙,生產(chǎn)環(huán)境下端口號(hào)為8000,開發(fā)環(huán)境下為8888智润。
我們?cè)赿emo項(xiàng)目的application.yml文件的同級(jí)目錄下創(chuàng)建兩個(gè)文件:
application-prod.yml:
server:
port: 8000
application-dev.yml:
server:
port: 8888
目錄結(jié)構(gòu)是這樣的:
application.yml
key:
name: cleverfan
sex: man
logging:
level: debug
spring:
profiles:
active: dev
啟動(dòng)項(xiàng)目:
端口號(hào)是8888
修改application.yml:
spring:
profiles:
active: prod
重新啟動(dòng)項(xiàng)目:
端口號(hào)的8000
證明我們的配置生效了及舍。
配置就先講到這里,如果有疑問或者錯(cuò)誤窟绷,請(qǐng)及時(shí)與我聯(lián)系锯玛。如果有需要補(bǔ)充的地方,也可以私戳我~