<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                Spring Expression Language (SpEL)是一種功能非常強大的表達式語言,可用于在運行時查詢和操作對象。 SpEL書寫在XML配置文件或者Annotation注解上,在Spring Bean的創建過程中生效。 SpEL能用在很多的場景下,在這篇文章中,給大家介紹一下在Spring Boot中如何使用SpEL表達式讀取配置屬性。 ## 一、使用SpEL表達式綁定字符串集合 創建一個配置文件employee.properties,內容如下: ~~~ employee.names=james,curry,zimug,姚明 employee.type=教練,球員,經理 employee.age={one:'27', two : '35', three : '34', four: '26'} ~~~ * 上文中names和type屬性分別代表雇員employee的名字和分類,是字符串類型屬性 * age屬性代表雇員的年齡,是一組鍵值對、類對象數據結構 創建一個配置類?`Employee`?,代碼如下: ~~~ @Data @Configuration @PropertySource (name = "employeeProperties", value = "classpath:employee.properties", encoding = "utf-8") public class Employee { //使用SpEL讀取employee.properties配置文件 @Value("#{'${employee.names}'.split(',')}") private List<String> employeeNames; } ~~~ * @Value注解和@PropertySource注解參考《YAML配置綁定變量的兩種方式》和《加載舊項目配置文件的兩種方式》學習 ## 二、測試用例 使用如下測試用例,將屬性值綁定到Employee類對象上,并將其打印 ~~~ @ExtendWith(SpringExtension.class) //@RunWith(SpringRunner.class) //Junit4開發者使用這個注解 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class ValueBindTests { @Resource Employee employee; @Test public void valueBindTests2() throws Exception { System.out.println(employee.toString()); } } ~~~ 輸出結果如圖所示,說明使用SpEL讀取employee.properties配置文件,并綁定屬性值到Employee 對象生效。 ![](https://img.kancloud.cn/c8/00/c800ca6c03bab0de0bf8354175eb1bcf_893x91.png) 上面的例子中,我們使用SpEL表達式讀取了employee.names屬性,并將其從字符串屬性,以逗號為分隔符轉換為List類型。屬性值注入完成之后,employeeNames=\[james, curry, zimug, 姚明\] > 如果出現中文亂碼,參考第四小節進行解決 ## 二、SpEL結合@Value注解讀取配置文件屬性--更多示例 * 假如我們需要獲取第一位(數組下標從0開始)雇員的姓名,可以使用如下的SpEL表達式: ~~~ @Value ("#{'${employee.names}'.split(',')[0]}") private String firstEmployeeName; ~~~ 屬性值注入完成之后,firstEmployeeName=‘’james‘’ * 我們還可以使用@Value注解將鍵值對、類對象的數據結構轉換為java的Map數據類型 ~~~ @Value ("#{${employee.age}}") private Map<String, Integer> employeeAge; ~~~ 屬性值注入完成之后,employeeAge={one=27, two=35, three=34, four=26} * 假如我們需要根據Map的Key獲取Value屬性值,可以使用如下的SpEL表達式: ~~~ @Value ("#{${employee.age}.two}") // @Value ("#{${employee.age}['two']}") //這樣寫也可以 private String employeeAgeTwo; ~~~ 屬性值注入完成之后,employeeAgeTwo=35 * 如果我們不確定,Map中的某個key是否存在,可以使用如下的SpEL表達式。如果key存在就獲取對應的value,如果不存在就獲得默認值31 ~~~ @Value ("#{${employee.age}['five'] ?: 31}") private Integer ageWithDefaultValue; ~~~ 屬性值注入完成之后,ageWithDefaultValue=31 ## 三、SpEL結合 @Value注解讀取系統環境變量 還可以使用SpEL表達式讀取系統環境變量,示例如下,獲取JAVA\_HOME目錄: ~~~ @Value ("#{systemProperties['java.home']}") private String javaHome; ~~~ 同理,可以獲取系統用戶工作目錄 ~~~ @Value ("#{systemProperties['user.dir']}") private String userDir; ~~~ 當然,除了以上在Spring Boot中使用SpEL的常用用法,SpEL還可以完成算術運算、邏輯運算、正則匹配運算、條件運算等功能。建議大家參照官方文檔學習。更多內容可以參考:[https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html](https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html) ## 四、讀取properties文件中文亂碼問題的解決 File->settings->File Encoding->圖所示選項及勾選 ![](https://img.kancloud.cn/da/9e/da9e0b9591666a9a9d5ac98f21592b30_1420x860.png) 使用PropertySource注解時指定encoding ![](https://img.kancloud.cn/e6/fa/e6fabe0c041e589a62c3054fc425f52c_783x126.png)
                  <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>

                              哎呀哎呀视频在线观看