轉載: https://www.cnblogs.com/duanxz/p/4520627.html
一、配置文件配置
直接配置
在src/main/resources下添加配置文件application.properties
例如修改端口號
#端口號 server.port=8089
分環境配置
在src/main/resources下添加,application-pro.properties,application-dev.properties和application.properties三個文件
application.properties
spring.profiles.active=dev
application-pro.properties
#端口號 server.port=80 #自定義端口號讀取 my.name=pzr.dev
application-dev.properties
#端口號 server.port=8089 #自定義端口號讀取 my.name=pzr.pro
當application.propertie設置spring.profiles.active=dev時,則說明是指定使用application-dev.properties文件進行配置
二、配置文件參數讀取
2.1、注解方式讀取
1、@PropertySource配置文件路徑設置,在類上添加注解,如果在默認路徑下可以不添加該注解。
需要用@PropertySource的有:
- 例如非application.properties,classpath:config/my.properties指的是src/main/resources目錄下config目錄下的my.properties文件,
- 例如有
多配置文件引用,若取兩個配置文件中有相同屬性名的值,則取值為最后一個配置文件中的值
在application.properties中的文件,直接使用@Value讀取即可,applicarion的讀取優先級最高
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"}) public class TestController
2、@Value屬性名,在屬性名上添加該注解
@Value("${my.name}") private String myName;
三、配置文件中配置集合類(Map、list)@Value注入map、List
yaml格式
@Value("#{'${list}'.split(',')}") private List<String> list; @Value("#{${maps}}") private Map<String,String> maps; @Value("#{${redirectUrl}}") private Map<String,String> redirectUrl;
配置文件
list: topic1,topic2,topic3 maps: "{key1: 'value1', key2: 'value2'}" redirectUrl: "{sso_client_id: '${id}',sso_client_secret: '${secret}',redirect_url: '${client.main.url.default}'}"
注意:
上面的list配置中,一定不要用“”把list所有的成員value包起來,要不然解析報錯。
上面的map配置中,一定要用“”把map所對應的value包起來,要不然解析會失敗,導致不能轉成 Map<String,String>。
因為yaml語法中如果一個值以 “{” 開頭, YAML 將認為它是一個字典, 所以我們必須引用它必須用”"
http://www.ansible.com.cn/docs/YAMLSyntax.html
yaml寫法注意:
字符串默認不用加上單引號或者雙引號
“”:雙引號;不會轉義字符串里面的特殊字符;特殊字符會作為本身想表示的意思
name: “zhangsan \n lisi”:輸出;zhangsan 換行 lisi
‘’:單引號;會轉義特殊字符,特殊字符最終只是一個普通的字符串數據
name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi
properties格式
示例1:使用@Value讀取application.properties里的配置內容
配置文件application.properties
spring.application.name=springbootdemo server.port=8080 mail.username=application-duan mail.password=application-duan123456
啟動類
package com.dxz.property5; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class TestProperty5 { public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty5.class).web(true).run(args); } }
測試類:
package com.dxz.property5; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") //@PropertySource("classpath:mail.properties") public class TaskController { @Value("${mail.username}") private String userName; @Value("${mail.password}") private String password; @RequestMapping(value = { "/", "" }) public String hellTask() { System.out.println("userName:" + userName); System.out.println("password:" + password); return "hello task !!"; } }
結果:
userName:application-duan password:application-duan123456
示例2:使用@Value+@PropertySource讀取其它配置文件(多個)內容
讀取mail.properties配置
package com.dxz.property5; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") @PropertySource("classpath:mail.properties") public class TaskController { @Value("${mail.smtp.auth}") private String userName; @Value("${mail.from}") private String password; @RequestMapping(value = { "/", "" }) public String hellTask() { System.out.println("userName:" + userName); System.out.println("password:" + password); return "hello task !!"; } }
結果:
userName:false password:me@localhost
2.2、對象映射方式讀取
- 首先建立對象與配置文件映射關系
- 方法中使用自動注入方式,將對象注入,調用get方法獲取屬性值
- 注意:新版本的@ConfigurationProperties沒有了location屬性,使用@PropertySource來指定配置文件位置
- prefix=”obj”指的是配置文件中的前綴,如obj.name,在定義對象屬性名時為private String name;
- 讀取配置文件中的集合時,使用List來接收數據,但List必須先實例化
測試類
package com.dxz.property6; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") @PropertySource({ "classpath:mail.properties", "classpath:db.properties" }) public class TaskController { // 在application.properties中的文件,直接使用@Value讀取即可,applicarion的讀取優先級最高 @Value("${mail.username}") private String myName; // 如果多個文件有重復的名稱的屬性話,最后一個文件中的屬性生效 @Value("${mail.port}") private String port; @Value("${db.username}") private String dbUserName; @Autowired ObjectProperties objectProperties; @RequestMapping("/test") @ResponseBody String test() { String result = "myName:" + myName + "\n port:" + port + "\n dbUserName:" + dbUserName + "\n objectProperties:" + objectProperties; System.out.println("result:=" + result); return result; } }
啟動類
package com.dxz.property6; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class TestProperty6 { public static void main(String[] args) { //SpringApplication.run(TestProperty1.class, args); new SpringApplicationBuilder(TestProperty6.class).web(true).run(args); } }
ObjectProperties.java
package com.dxz.property6; import java.util.ArrayList; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * 配置文件映射對象 * @author DELL */ @Component @PropertySource("classpath:config/object.properties") @ConfigurationProperties(prefix = "obj") public class ObjectProperties { private String name; private String age; // 集合必須初始化,如果找不到就是空集合,會報錯 private List<String> className = new ArrayList<String>(); public List<String> getClassName() { return className; } public void setClassName(List<String> className) { this.className = className; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "ObjectProperties [name=" + name + ", age=" + age + ", className=" + className + "]"; } }
object.properties
#自定義屬性讀取 obj.name=obj.name obj.age=obj.age obj.className[0]=obj.className[0] obj.className[1]=obj.className[1]
db.properties
db.username=admin db.password=admin123456 mail.port=2555
結果:http://localhost:8080/task/test/
result:=myName:application-duan port:2555 dbUserName:admin objectProperties:ObjectProperties [name=obj.name, age=obj.age, className=[obj.className[0], obj.className[1]]]