spring boot實戰(第四篇)分散配置
前言
分散配置是系統必不可少的一部分,將配置參數抽離出來爲後期維護提供很大的便利。spring boot 默認支持兩個格式的配置文件:.properties .yml。
.properties與.yml
*.properties屬性文件;屬於最常見的一種;
*.yml是yaml格式的文件,yaml是一種非常簡潔的標記語言。
在*.properties中定義user.address.stree=hangzhou
等價與yaml文件中的
user:
address:
stree:hangzhou
從上可以發現yaml層次感更強,具體在項目中選擇那種資源文件是沒有什麼規定的。
spring boot配置
簡單案例
首先在類路徑下創建application.properties
文件並定義
name=liaokailin
創建一個beanUser.java
@Component
public class User {
private @Value("${name:lkl}") String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
在 HelloWorldController.java
調用對應bean
package com.lkl.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.lkl.springboot.config.User;
import com.lkl.springboot.event.CallEventDemo;
@RestController
@RequestMapping("/springboot")
public class HelloWorldController {
@Autowired
CallEventDemo callEventDemo;
@Autowired
User user;
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
@ResponseBody
public String sayWorld(@PathVariable("name") String name) {
System.out.println("userName:" + user.getName());
return "Hello " + name;
}
}
啓動該spring boot工程,在命令行執行
curl http://localhost:8080/springboot/liaokailin
控制檯成功輸出name對應值
解析
- 在spring boot中默認會加載
classpath:/,classpath:/config/,file:./,file:./config/
路徑下以application
命名的property或yaml文件; - 參數
spring.config.location
設置配置文件存放位置 - 參數
spring.config.name
設置配置文件名稱
配置文件獲取隨機數
在spring boot配置文件中可調用Random
中方法
在application.properties
中爲user增加age參數 age=${random.int}
name=liaokailin
age=${random.int}
bean中同時增加參數
@Component
public class User {
private @Value("${name:lkl}") String name;
private @Value("${age}") Integer age;
//getter and setter and toString()
在啓動工程時會爲age隨機生成一個值
${random.int(100)} : 限制生成的數字小於10
${random.int[0,100]} : 指定範圍的數字
在配置文件調用佔位符
修改配置文件:
userName=liaokailin
age=${random.int[0,100]}
remark=hello,my name is ${userName},age is ${age}
修改bean:
@Component
public class User {
private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
執行發現remark答應出:
remark=hello,my name is liaokailin,age is 25
。
可以發現將name
修改爲userName
,在配置文件中調用${name}
是工程名。
去掉@Value
大家可以發現前面在bean中調用配置參數使用的爲註解@Value
,在spring boot中是可以省去該註解。
配置文件:
userName=liaokailin
age=${random.int[0,100]}
remark=hello,my name is ${userName},age is ${age}
user.address=china,hangzhou
增加user.address=china,hangzhou
,爲了調用該參數來使用@ConfigurationProperties
。
User.java
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
private String address;
使用@ConfigurationProperties
需要指定prefix
,同時bean中的屬性和配置參數名保持一致。
實體嵌套配置
在User中定義一個Address實體同樣可以快捷配置
User.java
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
private String address;
private Address detailAddress;
Address.java
“`
public class Address {
private String country;
private String province;
private String city;
...
}
``
application.properties`
userName=liaokailin
age=${random.int[0,100]}
remark=hello,my name is ${userName},age is ${age}
user.address=china,hangzhou
user.detailAddress.country=china
user.detailAddress.province=zhejiang
user.detailAddress.city=hangzhou
運行得到
userUser [name=liaokailin, age=57, remark=hello,my name is liaokailin,age is 0, address=china,hangzhou, detailAddress=Address [country=china, province=zhejiang, city=hangzhou]]
這種嵌套關係如果通過yaml文件展示出來層次感會更強。
user:
detailAddress:
country:china
province:zhejiang
city:hangzhou
注意在yaml中縮進不要使用TAB
配置集合
一個人可能有多個聯繫地址,那麼地址爲集合
User.java
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private @Value("${userName:lkl}") String name;
private @Value("${age}") Integer age;
private @Value("${remark}") String remark;
private String address;
private Address detailAddress;
private List<Address> allAddress = new ArrayList<Address>();
application.properties
user.allAddress[0].country=china
user.allAddress[0].province=zhejiang
user.allAddress[0].city=hangzhou
user.allAddress[1].country=china
user.allAddress[1].province=anhui
user.allAddress[1].city=anqing
```
通過`下標`表明對應記錄爲集合中第幾條數據,得到結果:
userUser [name=liaokailin, age=64, remark=hello,my name is liaokailin,age is 82, address=china,hangzhou, detailAddress=Address [country=china, province=zhejiang, city=hangzhou], allAddress=[Address [country=china, province=zhejiang, city=hangzhou], Address [country=china, province=anhui, city=anqing]]]
如果用yaml文件表示爲:
application.yml
user:
-allAddress:
country:china
province:zhejiang
city:hangzhou
-allAddress:
country:china
province:anhui
city:anqing
多配置文件
spring boot設置多配置文件很簡單,可以在bean上使用註解@Profile("development")
即調用application-development.properties|yml
文件,也可以調用SpringApplication
中的etAdditionalProfiles()
方法。
例如:
package com.lkl.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
import com.lkl.springboot.listener.MyApplicationStartedEventListener;
@Profile("development")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
// app.setAdditionalProfiles("development");
app.addListeners(new MyApplicationStartedEventListener());
app.run(args);
}
}
也可以通過啓動時指定參數spring.profiles.active
。
題外話
在實際項目中最好是將配置參數抽離出來集中管理,比如利用淘寶的super-diamond ,consul,zk 等。
轉載請註明
http://blog.csdn.net/liaokailin/article/details/48423847