## 一、使用@Value獲取配置值
通過@Value注解將family.family-name屬性的值綁定到familyName成員變量上面。
~~~
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
//當我們的類不屬于各種歸類的時候(不屬于@Controller、@Services等的時候),我們就可以使用@Component來標注這個類
public class Family {
@Value("${family.family-name}")
private String familyName;
}
~~~
在應用啟動的時候,從yml文件在讀取內容配置到變量中。
可通過測試用例來測試:
~~~
package com.kimgao.bootlauch;
import com.kimgao.bootlauch.model.Family;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
@Slf4j
@AutoConfigureMockMvc
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class ValueBeanTest {
@Resource
Family family;
@Test
public void valueBeanTest() throws Exception{
System.out.println(family.toString());
}
}
~~~
可看到測試用例打印出了配置文件的內容

## 二、使用@ConfigurationProperties獲取配置值
下面是用于接收上一節中yml配置的java實體類,先不要看我寫的代碼。測試一下,看看你自己能不能根據yml的嵌套結構,寫出來對應的java實體類:
~~~
// 1. 一個家庭有爸爸、媽媽、孩子。
// 2. 這個家庭有一個名字(family-name)叫做“happy family”
@Data
@Component
@ConfigurationProperties(prefix = "family") //表示配置的整體前綴
public class Family {
private String familyName; //成員變量名稱要和yml配置項key一一對應
private Father father;
private Mother mother;
private Child child;
}
~~~
~~~
// 3. 爸爸有名字(name)和年齡(age)兩個屬性
@Data
public class Father {
private String name;
private Integer age;
}
~~~
~~~
// 4. 媽媽有兩個別名
@Data
public class Mother {
private String[] alias;
}
~~~
~~~
//5. 孩子除了名字(name)和年齡(age)兩個屬性,還有一個friends的集合
@Data
public class Child {
private String name;
private Integer age;
private List<Friend> friends;
}
~~~
~~~
// 6. 每個friend有兩個屬性:hobby(愛好)和性別(sex)
@Data
public class Friend {
private String hobby;
private String sex;
}
~~~
## 三、測試用例
寫一個測試用例測試一下,看看yml配置屬性是否真的綁定到類對象的成員變量上面。
~~~
// @RunWith(SpringRunner.class) Junit4
@ExtendWith(SpringExtension.class) //Junit5
@SpringBootTest
public class CustomYamlTest {
@Autowired
Family family;
@Test
public void hello(){
System.out.println(family.toString());
}
}
~~~
測試結果,不能有為null的輸出字段,如果有表示你的java實體數據結構寫的不正確:
~~~
Family(familyName=happy family, father=Father(name=zimug, age=18),
mother=Mother(alias=[lovely, ailice]), child=Child(name=zimug2, age=5,
friends=[Friend(hobby=football, sex=male), Friend(hobby=basketball, sex=female)]))
~~~

## 四、比較一下二者
| | @ConfigurationProperties | @Value |
| --- | --- | --- |
| 功能 | 批量注入屬性到java類 | 一個個屬性指定注入 |
| 松散語法綁定 | 支持 | 不支持 |
| 復雜數據類型(對象、數組) | 支持 | 不支持 |
| JSR303數據校驗 | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
數據校驗和SPEL的內容,我們后面幾節會為大家介紹。
- 內容簡介
- 第一章 Spring boot 簡介
- 1.1 helloworld
- 1.2 提高開發效率工具lombok
- 1.3 IDEA熱部署
- 1.4 IDEA常用插件
- 1.5 常用注解
- 第二章 RESTful接口
- 2.1 RESTful風格API
- 2.1.1 spring常用注解開發RESTful接口
- 2.1.2 HTTP協議與Spring參數接收注解
- 2.1.3 Spring請求處理流程注解
- 2.2 JSON數據格式處理
- 2.2.1 Jackson的轉換示例代碼
- 2.3 針對接口編寫測試代碼
- 2.3.1 編碼接口測試示例代碼
- 2.3.2 帶severlet容器的接口測試示例代碼
- 2.3.3 Mockito測試示例代碼
- 2.3.4 Mockito輕量測試
- 2.4 使用swagger2構建API文檔
- 2.4.1 swagger2示例代碼
- 2.4.2 pom.xml
- 2.5 使用swagger2導出各種格式的接口文檔
- 第三章 sping boot配置管理
- 3.1 YAML語法
- 3.2 YAML綁定配置變量的方式
- 3.3 YAML配置屬性值校驗
- 3.4 YAML加載外部配置文件
- 3.5 SpEL表達式綁定配置項
- 3.6 不同環境下的多配置
- 3.7 配置文件的優先級
- 3.8 配置文件敏感字段加密
- 第四章 連接數據庫使用到的框架
- 4.1 spring JDBC
- 4.2 mybatis配置mybatisgenerator自動生成代碼
- 4.3 mybatis操作數據庫+dozer整合Bean自動加載
- 4.4 spring boot mybatis 規范
- 4.5 spirng 事務與分布式事務
- 4.6 spring mybaits 多數據源(未在git版本中實現)
- 4.7 mybatis+atomikos實現分布式事務(未在git版本中實現)
- 4.8 mybatis踩坑之逆向工程導致的服務無法啟動
- 4.9 Mybatis Plus
- 4.9.1.CURD快速入門
- 4.9.2.條件構造器使用與總結
- 4.9.3.自定義SQL
- 4.9.4.表格分頁與下拉分頁查詢
- 4.9.5.ActiveRecord模式
- 4.9.6.主鍵生成策略
- 4.9.7.MybatisPlus代碼生成器
- 4.9.8.邏輯刪除
- 4.9.9.字段自動填充
- 4.9.10.多租戶解決方案
- 4.9.11.雪花算法與精度丟失
- 第五章 頁面展現整合
- 5.1 webjars與靜態資源
- 5.2 模板引擎與未來趨勢
- 5.3 整合JSP
- 5.4 整合Freemarker
- 5.5 整合Thymeleaf
- 5.6 Thymeleaf基礎語法
- 5.7 Thymeleaf內置對象與工具類
- 5.8 Thymeleaf公共片段(標簽)和內聯JS
- 第六章 生命周期內的攔截、監聽
- 6.1 servlet與filter與listener的實現
- 6.1.1 FilterRegistration
- 6.1.2 CustomFilter
- 6.1.3 Customlister
- 6.1.4 FirstServlet
- 6.2 spring攔截器及請求鏈路說明
- 6.2.1 MyWebMvcConfigurer
- 6.2.2 CustomHandlerInterceptor
- 6.3 自定義事件的發布與監聽
- 6.4 應用啟動的監聽
- 第七章 嵌入式容器的配置與應用
- 7.1 嵌入式的容器配置與調整
- 7.2 切換到jetty&undertow容器
- 7.3 打war包部署到外置tomcat容器
- 第八章 統一全局異常處理
- 8.1 設計一個優秀的異常處理機制
- 8.2 自定義異常和相關數據結構
- 8.3 全局異常處理ExceptionHandler
- 8.3.1 HelloController
- 8.4 服務端數據校驗與全局異常處理
- 8.5 AOP實現完美異常處理方案
- 第九章 日志框架與全局日志管理
- 9.1 日志框架的簡介與選型
- 9.2 logback日志框架整合使用
- 9.3 log4j2日志框架整合與使用
- 9.4 攔截器實現用戶統一訪問日志
- 第十章 異步任務與定時任務
- 10.1 實現Async異步任務
- 10.2 為異步任務規劃線程池
- 10.3 通過@Scheduled實現定時任務
- 10.4 quartz簡單定時任務(內存持久化)
- 10.5 quartz動態定時任務(數據庫持久化)
- 番外章節
- 1.windows下安裝git
- 1 git的使用
- 2 idea通過git上傳代碼到github
- 2.maven配置
- 3.idea幾個輔助插件
- 4.idea配置數據庫
- 5.搭建外網穿透實現外網訪問內網項目
- 6.idea設置修改頁面自動刷新
- 7.本地tomcat啟動亂碼
- 8.win10桌面整理,得到一個整潔的桌面
- 9.//TODO的用法
- 10.navicat for mysql 工具激活
- 11.安裝redis
- 12.idea修改內存
- 13.IDEA svn配置
- 14.IntelliJ IDEA像Eclipse一樣打開多個項目