SpringBoot項目有一些基本的配置,比如啟動圖案(banner),比如默認配置文件application.properties,以及相關的默認配置項。
示例項目代碼在:
一、啟動圖案banner
編寫banner.txt放入resources文件夾下,然後啟動項目即可修改默認圖案。
關於banner的生成,可以去一些專門的網站。
比如:https://www.bootschool.net/ascii
二、配置文件application
2.1 application.properties/yml
resources下通常會默認生成一個application.properties文件,這個文件包含了SpringBoot項目的全局配置文件。裏面的配置項通常是這樣的:
server.port=8080
在這個文件里我們可以添加框架支持的配置項,比如項目端口號、JDBC連接的數據源、日誌級別等等。
現在比較流行的是將properties文件改為yml文件。yml文件的格式yaml是這樣的:
server:
port: 8080
yml和properties的作用是一樣的。而yml的好處是顯而易見的——更易寫易讀。
屬性之間互相調用使用${name}:
eknown:
email: eknown@163.com
uri: http://www.eknown.cn
title: 'hello, link to ${eknown.uri} or email to ${eknown.email}'
鏈接:
2.2 多環境配置文件
通常開發一個應用會有多個環境,常見如dev/prod,也會有test,甚至其他一些自定義的環境,SpringBoot支持配置文件的靈活切換。
定義新配置文件需要遵循以下格式:application-{profile}.properties
或者application-{profile}.yml
比如現在有dev和prod兩個環境,我需要在application.yml文件之外新建兩個文件:
application-dev.yml
server:
port: 8080
application-prod.yml
server:
port: 8081
然後在application.yml中通過application.profiles.active={profile}
指明啟用那個配置:
application:
profiles:
active: dev
除了在application.yml中指定配置文件外,還可以通過啟動命令指定:java -jar xxx.jar --spring.profiles.active=dev
2.2 自定義配置項並獲取它
主要介紹兩種方式,獲取單個配置項和獲取多個配置項。
舉例:
eknown:
email: eknown@163.com
uri: http://www.eknown.cn
2.2.1 使用@Value註解獲取單個配置項
@Value("${eknown.email}")
private String email;
@Value("${eknown.uri}")
private String url;
注意:使用@Value註解的時候,所在類必須被Spring容器管理,也就是被@Component、@Controller、@Service等註解定義的類。
2.2.2 獲取多個配置項
第一種,定義一個bean類,通過@Value獲取多個配置項:
@Component
public class MyConfigBean {
}
然後我們通過get方法來獲取這些值:
@RestController
public class BasicAction {
@Autowired
private MyConfigBean myConfigBean;
}
第二種,使用註解@ConfigurationProperties:
@Component
@ConfigurationProperties(perfix="eknown")
public class MyConfigBean {
private String email;
private String uri;
}
這裏只需要通過prefix指定前綴即可,後面的值自動匹配。
這裏我們還使用了@Component註解來讓spring容器管理這個MyConfigBean。
此外,我們可以不需要引入@Component,轉而在Application啟動類上加上@EnableConfigurationProperties({MyConfigBean.class})來啟動這個配置。
注意:我們這裡是從主配置文件,也就是SpringBoot默認的application-profile文件中獲取配置數據的。
而從自定義的配置文件,比如test.yml這種形式中獲取配置項時,情況是有點不大一樣的。
三、自定義配置文件
上面介紹的配置文件都是springboot默認的application開頭的文件。如果要自定義一個配置文件呢,比如test.yml或test.properties,怎麼獲取其中的配置項呢?
使用@PageResource註解即可。
首先我們來看一下讀取自定義的properties文件里的內容:
test.properties
hello.time=2019.11.19
hello.name=eknown
定義Configuration類:
@Configuration
@PropertySource("classpath:test.properties")
//@PropertySource("classpath:test.yml") // 注意,yml文件不能直接這樣寫,會讀不出數據
@ConfigurationProperties(prefix = "hello")
public class TestConfiguration {
private String name;
private String time;
// hide get and set methods
}
測試一下:
@RestController
@RequestMapping(value = "test")
public class TestAction {
@Autowired
private TestConfiguration testConfiguration;
@GetMapping(value = "config")
public String test() {
return testConfiguration.getName() + "<br/>" + testConfiguration.getTime();
}
}
如果將properties文件換成yml文件呢?
我們嘗試一下,發現:
讀不出數據?
分析一下@PropertySource註解,發現其使用的PropertySourceFactory是DefaultPropertySourceFactory.
這個類的源碼如下:
public class DefaultPropertySourceFactory implements PropertySourceFactory {
public DefaultPropertySourceFactory() {
}
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
}
}
這個類只能處理properties文件,無法處理yml文件。所以我們需要自定義一個YmlSourceFactory。
public class YamlSourceFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
return new YamlPropertySourceLoader().load(resource.getResource().getFilename()
, resource.getResource()).get(0);
}
}
然後定義test.yml文件的config類:
@Configuration
@PropertySource(value = "classpath:test.yml", encoding = "utf-8", factory = YamlSourceFactory.class)
@ConfigurationProperties(prefix = "yml.hello")
public class TestYamlConfiguration {
private String name;
private String time;
// hide get and set methods
}
注:為了區分test.properties和test.yml,這裏的test.yml中的屬性以yml.hello開頭。
編寫一下測試:
@Autowired
private TestYamlConfiguration ymlConfiguration;
@GetMapping(value = "yml")
public String testYml() {
return "yml config: <br/>" + ymlConfiguration.getName() + "<br/>" + ymlConfiguration.getTime();
}
訪問:
四、補充@ConfigurationProperties
網上一些資料中,為配合使用@ConfigurationProperties,還使用了@EnableConfigurationProperties註解。
經過測試發現:
本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
從SpringBoot默認配置文件讀取配置信息,使用@ConfigurationProperties + @Component/@Configuration,或者@ConfigurationProperties + 在啟動類添加@EnableConfigurationProperties({class})。這兩種方式都能解決問題
從非默認配置文件讀取配置信息,需要利用@PropertySource註解。同樣兩種方式:
2.1 @PropertySource + @ConfigurationProperties + @Component/@Configuration
2.2 @PropertySource + @ConfigurationProperties + @Component/@Configuration + @EnableConfigurationProperties,第二種方式存在一個問題,即還是必須要使用@Component註解,如果不使用,則會導致讀取配置信息為null,但程序不會報錯;而如果採用了,則會導致bean類的set方法被執行兩次(也就是生成了兩個同樣類型的bean類)。這種方式不建議!
【其他文章推薦】
※為什麼 USB CONNECTOR 是電子產業重要的元件?
※網頁設計一頭霧水??該從何著手呢? 找到專業技術的網頁設計公司,幫您輕鬆架站!
※想要讓你的商品成為最夯、最多人討論的話題?網頁設計公司讓你強力曝光
※想知道最厲害的台北網頁設計公司推薦、台中網頁設計公司推薦專業設計師"嚨底家"!!
※專營大陸快遞台灣服務
※台灣快遞大陸的貨運公司有哪些呢?
Orignal From: SpringBoot基本配置詳解
留言
張貼留言