作者:追夢1819
原文:https://www.cnblogs.com/yanfei1819/p/10837594.html
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
前言
SpringBoot 完全摒棄了xml配置的模式,幾乎做到了“零配置”。說是“幾乎”,是因為一般情況下默認的配置足夠滿足日常開發所需,但是在特殊的情況下,我們往往需要用到自定義屬性配置、自定義文件配置、多環境配置、外部命令引導等一系列功能。
SpringBoot 使用的全局配置文件 application.properties 的作用是對一些默認配置的值進行修改。配置文件通常放在src/main/resources目錄下或者類路徑的/config下。application.properties 是springboot 項目的主配置文件。
本節內容包括:
- 主要配置文件
- 默認配置
- 自定義配置文件
- 多環境配置
- 外部配置
## 主配置文件
application.properties 文件是作為 SpringBoot 項目的默認配置文件。項目啟動,它會被自動檢測到。 然后我們可以正常注入任何加載的屬性。因此,通過使用此默認文件,我們不必顯式注冊PropertySource,甚至不必提供屬性文件的路徑。
當然,如果必要的話,我們可以在項目運行時改變主配置文件,以下是命令:
java -jar config-demo.jar --spring.config.location=classpath:/customeize-application.properties
customeize-application.properties 是定義在 src/main/resources 路徑下的配置文件。
而此時,application.properties 則不再起作用。如果項目中使用到了該文件中的屬性,則在項目啟動時可能會報錯。需要特別注意。
## 默認配置
默認屬性
SpringBoot 的默認屬性有很多,如以上所說,一般情況下默認的配置足夠滿足日常開發所需。
針對 SpringBoot 的默認屬性,我們可以參照官方文檔:
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
自定義屬性
准備工作
在介紹自定義屬性之前,為了提升開發者的體驗(所以不是必須的依賴),先來做一下准備工作,那就是引入 maven 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
因為,在常用的IDE中,不會有屬性提示。該依賴只會在編譯時調用,所以不用擔心會對生產造成影響。
自定義屬性
通常情況下,一些常用的屬性,可以直接在 SpringBoot 的主配置文件 application.properties 中自定義。
例如:
server.port=8083
# 自定義屬性(單個)
yanfei1819.name=admin
yanfei1819.age=26
讀取以上配置信息:
package com.yanfei1819.configdemo.config;
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;
/**
* Created by 追夢1819 on 2019-05-06.
*/
@Controller
public class ReadApplicationConfigController {
@Value("${yanfei1819.name}")
private String name;
@Value("${yanfei1819.age}")
private int age;
@ResponseBody
@RequestMapping("/getProperties")
public String getProperties(){
return "我的姓名是:"+name+",我的年齡是:"+age;
}
}
用postman進行測試:
以上是讀取單個屬性,如果屬性比較多,這樣就比較麻煩了,也不符合面向對象的思想。下面通過對象來讀取配置信息。
首先,在 application.properties 中配置。
# 演示自定義對象
class.student.name=zhangsan
class.student.age=20
class.student.grade=98.5
其次,創建實體類接受對應的屬性值。
package com.yanfei1819.configdemo.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by 追夢1819 on 2019-05-06.
*/
@ConfigurationProperties(prefix = "class.student")
public class Student {
private String name;
private int age;
private Double grade;
// set/get 省略
}
注意,在啟動類上添加。
@EnableConfigurationProperties({Student.class})
最后,測試:
package com.yanfei1819.configdemo.config;
import com.yanfei1819.configdemo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* Created by 追夢1819 on 2019-05-06.
*/
@Controller
public class ReadApplicationConfigController {
@Autowired
private Student student;
@ResponseBody
@RequestMapping("/getBeanProperties")
public String getBeanProperties(){
return "學生姓名是:"+student.getName()+",學生年齡是:"+student.getAge()+",學生分數是:"+student.getGrade();
}
}
測試結果是。
參數間的相互引用
class.student.description=姓名是${class.student.name},年齡是${class.student.age},分數是${class.student.grade}
然后同其他的屬性方式引用。
自定義配置文件
一些特殊情況,需要的配置信息很多,如果全部定義在主配置文件中,會繁雜、難以維護。這個時候就需要自定義一些配置,將屬性進行分類,便於維護。以下以JDBC配置文件為例,闡述自定義配置文件以及屬性值的讀取方式。
首先,創建自定義配置文件 jdbc.properties 。
jdbc.mysql.driverclassname=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://127.0.0.1:3306/xxx
jdbc.mysql.username=root
jdbc.mysql.password=root
其次,創建對應的實體類。
package com.yanfei1819.configdemo.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* Created by 追夢1819 on 2019-05-06.
*/
@Configuration
@ConfigurationProperties(prefix = "jdbc.mysql")
@PropertySource("classpath:jdbc.properties")
public class JdbcBean {
private String driverclassname;
private String url;
private String username;
private String password;
// set/get/toString 省略
}
最后,創建測試類。
package com.yanfei1819.configdemo.config;
import com.yanfei1819.configdemo.entity.JdbcBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 追夢1819 on 2019-05-06.
*/
@RestController
public class ReadCustomPropertiesController {
@Autowired
private JdbcBean jdbcBean;
@GetMapping("/getJdbcBean")
private String getJdbcBean(){
return jdbcBean.toString();
}
}
啟動程序,可見測試結果。
注意,一個實體類的配置文件可以有多個。換句話說,可以用一個實體類接收多個配置文件的信息。
我們把上述demo的基礎上,新增一個配置文件 jdbc2.properties ,並添加屬性:
jdbc.mysql.ip=127.0.0.1
然后在實體類 JdbcBean 中加入屬性:
private String ip;
// get/set/toString 省略
同時添加注解:
@ConfigurationProperties(prefix = "jdbc2.mysql")
當然,上面的兩個注解:
@PropertySource("classpath:jdbc.properties")
@PropertySource("classpath:jdbc2.properties")
也可以用以下注解代替:
@PropertySources({
@PropertySource("classpath:jdbc.properties"),
@PropertySource("classpath:jdbc2.properties")
})
只是需要注意的是,在任何一種情況下,值得注意的是,在屬性名稱沖突的情況下,最后一次源讀取優先。
## 多環境配置
上述內容只是演示了最基本的功能。而在真是的項目中,往往有多個環境,比如開發、測試、生產的環境。可能每個環境的配置參數都不一樣。這個時候也需要我們自定義每個環境的配置文件了。
不過,這不需要以上那么復雜。因為SpringBoot 已經為我們做好了准備工作。只要遵循它提供的配置規則和格式即可。
1. 各個環境的配置文件格式是:application-{profile}.properties ;
2. 在主配置文件中添加屬性 spring.profile.active
即可。
例如:
生產環境配置文件:application-prod.properties
開發環境配置文件:application-dev.properties
引用開發環境的配置文件,只要在 application.properties 中配置: spring.profiles.active=dev
。
外部配置
除了以上幾種配置方式。SpringBoot 還支持外部命令行配置。
例如,在啟動時修改程序端口號:java -jar xx.jar --server.port=9090
;
選擇對應環境配置:java -jar xx.jar --spring.profiles.active=test
等。其余還有很多。由於篇幅所限,此處不一一展開。感興趣的可以自行深入了解。
源碼:我的GitHub