前言
算是對《SpringBoot初學(2) - properties配置和讀取》的總結吧。
概念性總結
一、Spring Boot允許外化(externalize)你的配置。可以使用properties文件,YAML文件,環境變量和命令行參數來外化配置。
使用@Value注解,可以直接將屬性值注入到你的beans中,並通過Spring的Environment抽象或綁定到結構化對象來訪問。
二、Spring Boot使用一個非常特別的PropertySource次序來允許對值進行合理的覆蓋,需要以下面的次序考慮屬性:
(盡量能理解了加載順序在記憶,不強求記住。)
1. 命令行參數
2. 來自於java:comp/env的JNDI屬性
3. Java系統屬性(System.getProperties())
4. 操作系統環境變量
5. 只有在random.*里包含的屬性會產生一個RandomValuePropertySource
6. 在打包的jar外的應用程序配置文件(application.properties,包含YAML和profile變量)
7. 在打包的jar內的應用程序配置文件(application.properties,包含YAML和profile變量)
8. 在@Configuration類上的@PropertySource注解
9. 默認屬性(使用SpringApplication.setDefaultProperties指定)
三、YAML相對properties的缺點
YAML文件不能通過@PropertySource注解加載。所以,在這種情況下,如果需要使用@PropertySource注解的方式加載值,那就要使用properties文件。
四、Spring boot類型安全的配置屬性
在spring提供的讀取properties中,運用注解@Value("${property}")注解注入配置屬性有時可能比較笨重,特別是需要使用多個properties或你的數據本身有層次結構。
為了控制和校驗你的應用配置,Spring Boot提供一個允許強類型beans的替代方法來使用properties。
當@EnableConfigurationProperties注解應用到你的@Configuration時,任何被@ConfigurationProperties注解的beans將自動被Environment屬性配置。
這種風格的配置特別適合與SpringApplication的外部YAML配置進行配合使用。
五、Spring boot的松散綁定
(摘自:spring boot參考指南,101/420,23.7.2)
(暫時也沒理解ConversionService到底要怎么寫。)
一、java基本類型的properties讀取
java的8種基本類型:邏輯型boolean、文本型char、整數型(byte、short、int、long)、浮點型(float、double)。對應的封裝類型如Integer、Boolean等是一樣的。
特殊類型String。
## BaseProperty.properties byte_=1 short_=2 int_=23 long_=1234 float_=123.456 double_=12345.6789 char_=2 boolean_=true ## 中文亂碼問題,不同的IDE問題處理不一樣。 string_=str中文 ## 日期不知如何直接注入成Date類型 date_=2017-02-13
package com.vergilyn.demo.springboot.properties.bean; import javax.validation.constraints.NotNull; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Configuration @ConfigurationProperties() @PropertySource("classpath:config/properties/BaseProperty.properties") @Component public class BasePropertyBean { private byte byte_ ; private short short_ ; private int int_ ; private long long_ ; private float float_ ; private double double_ ; private char char_ ; private boolean boolean_ ; @NotNull //加JSR-303 javax.validation約束注解 private String string_; /* 不知道怎么直接注入Date類型*/ // private Date date_; // 省略set/get }
二、復雜類型
如Map,List,數組等。
## ComplexProperty.properties ## map vergilyn.map[blog]=http://www.cnblogs.com/VergiLyn/ vergilyn.map[name]=VergiLyn vergilyn.map[remark]=備注,中文23333 ## list vergilyn.list[0]=Carpenters vergilyn.list[1]=Celine Dion vergilyn.list[2]=Bon Jovi vergilyn.list[3]=Taylor Swift ## array vergilyn.array[0]=I Don't Wanna Live Forever vergilyn.array[1]=Safe And Sound vergilyn.array[2]=22 vergilyn.array[4]=Unpredictable Story vergilyn.array[8]=No One vergilyn.str=string vergilyn.num=124 vergilyn.date=2017-01-14 23:55:19 vergilyn.isSuccess=false
package com.vergilyn.demo.springboot.properties.bean; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Configuration @ConfigurationProperties(prefix = "vergilyn") @PropertySource("classpath:config/properties/ComplexProperty.properties") @Component public class ComplexPropertyBean { private Map<String, Object> map; private List<String> list; private String[] array; //省略 set/get }
三、隨機數
如果要知道詳細的,可以自行baidu、google。
## RandomPropertyBean.properties ## 隨機值注入 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]}
package com.vergilyn.demo.springboot.properties.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Configuration @ConfigurationProperties() @PropertySource("classpath:config/properties/RandomProperty.properties") @Component public class RandomPropertyBean { @Value("${my.secret}") private String secret; @Value("${my.number}") private int number; @Value("${my.bignumber}") private long bignumber; @Value("${my.number.less.than.ten}") private int intten; @Value("${my.number.in.range}") private int range; // 省略 set/get }
(注意可能需要在application.properties中配置@Value常量。詳見github源碼。)

package com.vergilyn.demo.springboot.properties; import com.vergilyn.demo.springboot.properties.bean.BasePropertyBean; import com.vergilyn.demo.springboot.properties.bean.ComplexPropertyBean; import com.vergilyn.demo.springboot.properties.bean.RandomPropertyBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Configuration @ComponentScan @EnableAutoConfiguration @PropertySource(value={"classpath:config/application.properties"} , ignoreResourceNotFound = true) @Controller public class PropertyApplication { @Autowired BasePropertyBean base; @Autowired ComplexPropertyBean complex; @Autowired RandomPropertyBean random; // @Autowired // RelaxedBindPropertyBean relaxed; @Value("${CONSTANT_PASSWORD}") private String password; public static void main(String[] args) { SpringApplication.run(PropertyApplication.class, args); } @RequestMapping("/property") public String property(@RequestParam(value="name", required=false, defaultValue="${CONSTANT_USER}") String name , Model model) { model.addAttribute("name", name); model.addAttribute("password", password); System.out.println(base); System.out.println(complex); System.out.println(random); // System.out.println(relaxed); return "custom"; } }
github :
https://github.com/vergilyn/SpringBootDemo
自己學習spring boot的項目,並不是像官方samples一樣是一個功能是一個項目,我是把所有都放在一個項目。
所以注意看application-{profile}.properties和application.properties的配置。
因為是一個項目,所以pom引入了很多jar。所以,可能有些properties配置是必須的。