使用配置文件注入屬性
Spring Boot 默認的配置文件src/main/java/resources/application.properties或者src/main/java/resources/application.yml,在這裏我們可以配置一些常量。
首先我們使用配置文件給一個類注入相關的屬性:
com.xiaohang.controller.pet.no = ${random.uuid}
com.xiaohang.controller.pet.name = Tom
通過註解@Value(value=”${config.name}”)就可以綁定到你想要的屬性上面。
package com.xiaohang.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/pet")
public class PetController {
@Value(value = "${com.xiaohang.controller.pet.no}")
private String no;
@Value(value = "${com.xiaohang.controller.pet.name}")
private String name;
@RequestMapping("/d")
public String getDeatils() {
return "no: " + no + ", name: " + name;
}
}
一個個綁定數據還是很不方便,可以新建一個Bean,專門用來綁定注入的屬性使用註解@ConfigurationProperties(prefix = “prefix”),不過需要注意的是先要引入相關依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
通過使用spring-boot-configuration-processor jar,你可以從被@ConfigurationProperties註解的節點輕鬆的產生自己的配置元數據文件。
這裏我新建一個PetBean用來注入屬性。
package com.xiaohang.demo.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.xiaohang.controller.pet")
@Data
public class PetBean {
private String no;
private String name;
}
使用配置文件給一個類注入相關的屬性:
com.xiaohang.controller.pet.no = ${random.uuid}
com.xiaohang.controller.pet.name = Tom
注意在啓動類上加上註解
@EnableConfigurationProperties({PetBean.class}),
Application.java
package com.xiaohang.demo;
import com.xiaohang.demo.bean.PetBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties(value = PetBean.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
新建一個controller,注入我們創建的PetBean
package com.xiaohang.demo.controller;
import com.xiaohang.demo.bean.PetBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v2/pet")
public class PetController1 {
@Autowired
private PetBean pet;
@RequestMapping("/d")
public String getDetail() {
return "no: " + pet.getNo() + ", name: " + pet.getName();
}
}
使用自定義的配置文件
我們在resouce目錄下面創建一個bean/pet.properties,加入
com.xiaohang.no = 123456
com.xiaohang.name = Tom
新建一個PetBean1.java:
@PropertySource 這個註解可以指定具體的屬性配置文件,優先級比較低。
package com.xiaohang.demo.bean;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "com.xiaohang")
@PropertySource(value = "classpath:bean/pet.properties")
@Data
public class PetBean1 {
private String no;
private String name;
}
在controller中加入PetBean1的注入
package com.xiaohang.demo.controller;
import com.xiaohang.demo.bean.PetBean1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("v1/pet")
public class PetController2 {
@Autowired
private PetBean1 pet1;
@RequestMapping("d1")
public String getDetails() {
return "no: " + pet1.getNo() + ", name: " + pet1.getName();
}
}
隨機數
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
屬性佔位符
app.name=MyApp
app.description=${app.name} is a Spring Boot application
可以在配置文件中引用前面配置過的屬性(優先級前面配置過的這裏都能用)。
通過如${app.name:默認名稱}方法還可以設置默認值,當找不到引用的屬性時,會使用默認的屬性。
屬性名匹配規則
例如有如下配置對象:
@Component
@ConfigurationProperties(prefix="person")
public class ConnectionSettings {
private String firstName;
}
firstName可以使用的屬性名如下:
- person.firstName,標準的駝峯式命名
- person.first-name,虛線(-)分割方式,推薦在.properties和.yml配置文件中使用
- PERSON_FIRST_NAME,大寫下劃線形式,建議在系統環境變量中使用
配置文件的優先級
Spring Boot 支持多種外部配置方式,這些方式優先級如下:
- 命令行參數
- 來自java:comp/env的JNDI屬性
- Java系統屬性(System.getProperties())
- 操作系統環境變量
- RandomValuePropertySource配置的random.*屬性值
- jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
- jar包內部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
- jar包外部的application.properties或application.yml(不帶spring.profile)配置文件
- jar包內部的application.properties或application.yml(不帶spring.profile)配置文件
- @Configuration註解類上的@PropertySource
- 通過SpringApplication.setDefaultProperties指定的默認屬性
同樣,這個列表按照優先級排序,也就是說,src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,此外,如果你在相同優先級位置同時有application.properties和application.yml,那麼application.properties裏的屬性裏面的屬性就會覆蓋application.yml。
Profile-多環境配置
當應用程序需要部署到不同運行環境時,一些配置細節通常會有所不同,最簡單的比如日誌,生產日誌會將日誌級別設置爲WARN或更高級別,並將日誌寫入日誌文件,而開發的時候需要日誌級別爲DEBUG,日誌輸出到控制檯即可。
如果按照以前的做法,就是每次發佈的時候替換掉配置文件,這樣太麻煩了,Spring Boot的Profile就給我們提供瞭解決方案,命令帶上參數就搞定。
這裏我們來模擬一下,只是簡單的修改端口來測試
在Spring Boot中多環境配置文件名需要滿足application-{profile}.properties的格式,其中{profile}對應你的環境標識,比如:
application-dev.properties:開發環境
application-prod.properties:生產環境
然後在application.properties中加入
spring.profiles.active=dev
或application.yml中加入
spring:
# 環境 dev|test|pro
profiles:
active: dev
或啓動命令
java -jar xxx.jar --spring.profiles.active=dev
參數用–xxx=xxx的形式傳遞。意思就是表示在application.properties文件中配置了屬性。
可以通過SpringApplication.setAddCommandLineProperties(false)禁用命令行配置。