<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一、使用@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()); } } ~~~ 可看到測試用例打印出了配置文件的內容 ![](https://img.kancloud.cn/4a/22/4a22af967440dd1fe0c7c267c395be05_1135x354.png) ## 二、使用@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)])) ~~~ ![](https://img.kancloud.cn/b1/e6/b1e6c3c6999dc5fecafb82a7535077d1_1826x287.png) ## 四、比較一下二者 | | @ConfigurationProperties | @Value | | --- | --- | --- | | 功能 | 批量注入屬性到java類 | 一個個屬性指定注入 | | 松散語法綁定 | 支持 | 不支持 | | 復雜數據類型(對象、數組) | 支持 | 不支持 | | JSR303數據校驗 | 支持 | 不支持 | | SpEL | 不支持 | 支持 | 數據校驗和SPEL的內容,我們后面幾節會為大家介紹。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看