1. SpringBoot配置文件類型
1.1 SpringBoot配置文件類型和作用
SpringBoot是基于約定的,所以很多配置都有默認(rèn)值,但如果想使用自己的配置替換默認(rèn)配置的話,就可以使用application.properties或者application.yml(application.yaml)進(jìn)行配置。
SpringBoot默認(rèn)會從Resources目錄下加載application.properties或application.yml(application.yaml)文件
其中,application.properties文件是鍵值對類型的文件,之前一直在使用,所以此處不在對properties文件的格式進(jìn)行闡述。除了properties文件外,SpringBoot還可以使用yml文件進(jìn)行配置。
application.properties
#server.port=8081
server.servlet.context-path=/demo
1.2 application.yml配置文件
1.2.1 yml配置文件簡介
YML文件格式是YAML (YAML Aint Markup Language)編寫的文件格式,YAML是一種直觀的能夠被電腦識別的的數(shù)據(jù)數(shù)據(jù)序列化格式,并且容易被人類閱讀,容易和腳本語言交互的,可以被支持YAML庫的不同的編程語言程序?qū)耄热纾?C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以數(shù)據(jù)為核心的,比傳統(tǒng)的xml方式更加簡潔。
YML文件的擴展名可以使用.yml或者.yaml。
1.2.2 yml配置文件的語法
- 配置普通數(shù)據(jù)
語法: key: value
示例代碼:
name: haohao
注意:value之前有一個空格
- 配置對象數(shù)據(jù)
語法:
key
key1: value1
key2: value2
或者:
key: {key1: value1,key2: value2}
示例代碼:
person:
name: haohao
age: 31
addr: beijing
#或者
#行內(nèi)對象配置
person: {name: haohao,age: 31,addr: beijing}
注意:key1前面的空格個數(shù)不限定,在yml語法中,相同縮進(jìn)代表同一個級別
- 配置Map數(shù)據(jù)
同上面的對象寫法
#Map配置
map:
key1: value1
key2: value2
- 配置數(shù)組(List、Set)數(shù)據(jù)
語法:
key:
- value1
- value2
或者:
key: [value1,value2]
示例代碼:
city:
- beijing
- tianjin
- shanghai
- chongqing
#或者
city: [beijing,tianjin,shanghai,chongqing]
#集合中的元素是對象形式
student:
- name: zhangsan
age: 18
score: 100
- name: lisi
age: 28
score: 88
- name: wangwu
age: 38
score: 90
#student: [{name: tom,age: 18,addr: beijing},{name: lucy,age: 17,addr: tianjin}]
注意:value1與之間的 - 之間存在一個空格
1.3 SpringBoot配置信息的查詢
上面提及過,SpringBoot的配置文件,主要的目的就是對配置信息進(jìn)行修改的,但在配置時的key從哪里去查詢
呢?我們可以查閱SpringBoot的官方文檔
文檔URL:https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#common-application-properties
常用的配置摘抄如下:
# QUARTZ SCHEDULER (QuartzProperties)
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.
sql # Path to the SQL file to use to initialize the database schema.
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
# ----------------------------------------
# WEB PROPERTIES
# ----------------------------------------
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080 # Server HTTP port.
server.servlet.context-path= # Context path of the application.
server.servlet.path=/ # Path of the main dispatcher servlet.
# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to
the "Content-Type" header if not set explicitly.
# JACKSON (JacksonProperties)
spring.jackson.date-format= # Date format string or a fully-qualified date format
class name. For instance, `yyyy-MM-dd HH:mm:ss`.
# SPRING MVC (WebMvcProperties)
spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher
servlet.
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-
detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
# JEST (Elasticsearch HTTP client) (JestProperties)
spring.elasticsearch.jest.password= # Login password.
spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use.
spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use.
spring.elasticsearch.jest.read-timeout=3s # Read timeout.
spring.elasticsearch.jest.username= # Login username.
我們可以通過配置application.poperties 或者 application.yml 來修改SpringBoot的默認(rèn)配置
例如:
application.properties文件
server.port=8888
server.servlet.context-path=demo
application.yml文件
server:
port: 8888
servlet:
context-path: /demo
2. 配置文件與配置類的屬性映射方式
2.1 使用注解@Value映射
我們可以通過@Value注解將配置文件中的值映射到一個Spring管理的Bean的字段上
例如:
application.properties配置如下:
person:
name: zhangsan
age: 18
或者,application.yml配置如下:
person:
name: zhangsan
age: 18
實體Bean代碼如下:
package com.neuedu.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Quick2Controller {
@Value("${name}")
private String name;
@RequestMapping("/quick2")
@ResponseBody
public String quick2(){
//獲得配置文件的信息
return "name:" + name;
}
}
瀏覽器訪問地址:http://localhost:8081/demo/quick2 結(jié)果如下:
2.2 使用注解@ConfigurationProperties映射
通過注解@ConfigurationProperties(prefix="配置文件中的key的前綴")可以將配置文件中的配置自動與實體進(jìn)行映
射
application.properties配置如下:
person:
name: zhangsan
age: 18
或者,application.yml配置如下:
person:
name: zhangsan
age: 18
實體Bean代碼如下:
@Controller
@ConfigurationProperties(prefix = "person")
public class QuickStartController {
private String name;
private Integer age;
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "springboot 訪問成功! name="+name+",age="+age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
}
瀏覽器訪問地址:http://localhost:8080/quick 結(jié)果如下:
注意:使用@ConfigurationProperties方式可以進(jìn)行配置文件與實體字段的自動映射,但需要字段必須提供set方法才可以,而使用@Value注解修飾的字段不需要提供set方法