-----崩了,因為yml測試多次始終失敗,以後如果使用屬性檔案我就用properties---
當今Java開發使用屬性檔案已經非常普遍,是以談談這方面的内容。在Spring Boot中使用屬性檔案,可以采用預設為我們準備的application.properties或者application.yml,也可以使用自定義的配置檔案。應該說讀取配置檔案的方法很多,隻是介紹最常用的方法。
在Spring Boot中,我們先在Maven配置檔案中加載依賴,這樣Spring Boot将建立讀取屬性檔案的上下文。
<!--屬性檔案依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
有了依賴就可以直接使用屬性配置檔案為你工作了,建立一個app.properties檔案。例如現在為他新增代碼清單所示的屬性。
datasource.driverName=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://172.17.0.1:3306/hc_official_website_1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
datasource.username=root
datasource.password=123456
使用@PropertySource作為加載的屬性配置檔案,他會通過其機制讀取到上下文中,這樣就可以引用他了。對于他的引用,有兩種方法,首先是Spring表達式。本節我們隻是涉及讀取屬性不涉及運算。先建立一個新的類DataBaseProperties
(使用yml已經不行了)
/**
* 使用屬性設定
*/
@Component
@PropertySource("classpath:app.properties")
public class DataBaseProperties {
@Value("${datasource.driverName}")
private String driverName=null;
@Value("${datasource.url}")
private String url = null;
private String username=null;
private String password=null;
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
System.out.println(driverName);
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
System.out.println(url);
this.url = url;
}
public String getUsername() {
return username;
}
@Value("${datasource.username}")
public void setUsername(String username) {
System.out.println(username);
this.username = username;
}
public String getPassword() {
return password;
}
@Value("${datasource.password}")
public void setPassword(String password) {
System.out.println(password);
this.password = password;
}
}
這樣我們就可以通過@Value注解,使用${…}這樣的占位符讀取配置在屬性檔案的内容。這裡的@Value注解,既可以加載屬性,也可以加在方法上,啟動Spring Boot就可以看到下面的日志了。
12:05:30.058 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataBaseProperties'
12:05:30.061 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'datasource.driverName' in PropertySource 'class path resource [app.properties]' with value of type String
12:05:30.062 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'datasource.url' in PropertySource 'class path resource [app.properties]' with value of type String
12:05:30.062 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'datasource.username' in PropertySource 'class path resource [app.properties]' with value of type String
root
12:05:30.062 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'datasource.password' in PropertySource 'class path resource [app.properties]' with value of type
最後還得使用注解@ConfigurationProperties,通過他使得配置上有所減少,例如,修改如下代碼
package cn.hctech2006.boot.bootall.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 使用屬性設定
*/
@Component
@PropertySource(value = {"classpath:app.properties"},ignoreResourceNotFound = true)
@ConfigurationProperties("datasource")
@EnableConfigurationProperties
public class DataBaseProperties {
private String driverName=null;
private String url = null;
private String username=null;
private String password=null;
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
System.out.println(driverName);
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
System.out.println("url:"+url);
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
System.out.println(username);
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
System.out.println(password);
this.password = password;
}
}
這裡在注解@ConfigurationProperties中配置的字元串datasource,将與POJO的屬性名稱組成屬性的全限定名去配置檔案中尋找,這樣就能将對應的屬性讀取到POJO中。然後使用@PropertySource去定義對應的屬性檔案,把他加載到Spring的上下文中。
在@PropertySource注解中,value可以配置多個配置檔案。使用classpath字首,意味着去類檔案路徑下找到屬性檔案;ignoreResourceNotFound則是忽略配置檔案找不到的問題。ignoreResourceNotFound的預設值為flase,也就是找不到屬性檔案回報錯;這裡配置為true,也就是找不到也不會報錯
----------------屬性檔案加載一直是一個挺困擾人的問題,就是@ConfigurationProperties完全不起作用,另外yml使用讀取的時候,縮進也不起作用-------------