<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 一、為什么要學加載外部配置文件 本節為大家介紹另外一種場景:有一些老的項目里面的jar包并未主動的去與spring boot 融合,很多jar包都有自己的配置文件。如果我們在spring boot項目中使用這些jar包就必須得使用它們的配置文件,那就面臨一個問題:**我們的spring boot項目默認只有一個全局配置文件:application.yml或application.properties。該如何加載額外的配置文件?** 如何解決這個問題,就是本節將為大家介紹的內容。 ## 二、使用@PropertySource加載自定義yml或properties文件 #### 2.1.properties配置文件加載 `family.properties`這種格式的配置文件,使用如下的注解就可以將文件中的配置屬性進行加載,非常簡單! ~~~ @PropertySource(value = {"classpath:family.properties"}) public class Family { ~~~ #### 2.2.yaml配置文件加載 [spring 官方文檔明確說明不支持使用@PropertySource加載YAML配置文件](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-yaml-shortcomings),但是我們仍然有辦法,跟著我繼續。 ![](https://img.kancloud.cn/64/a6/64a63bd31fad31bb15c78b8231abe484_1204x77.png) * 我們新建一個配置文件family.yml,把上一節用到的YAML數據結構放里面。用來模擬第三方jar包的額外配置文件(非application配置文件)。 ~~~ # 1. 一個家庭有爸爸、媽媽、孩子。 # 2. 這個家庭有一個名字(family-name)叫做“happy family” # 3. 爸爸有名字(name)和年齡(age)兩個屬性 # 4. 媽媽有兩個別名 # 5. 孩子除了名字(name)和年齡(age)兩個屬性,還有一個friends的集合 # 6. 每個friend有兩個屬性:hobby(愛好)和性別(sex) family: family-name: "happy family" father: name: zimug age: 18 mother: alias: - lovely - ailice child: name: zimug2 age: 5 friends: - hobby: football sex: male - hobby: basketball sex: female ~~~ * 筆者通過閱讀代碼了解到,DefaultPropertySourceFactory是進行配置文件加載的工廠類。 * 盡管其默認不支持讀取YAML格式外部配置文件,但是我們可以通過繼承DefaultPropertySourceFactory ,然后對它的createPropertySource進行一下改造。就可以實現YAML的“額外”配置文件加載。 ~~~ public class MixPropertySourceFactory extends DefaultPropertySourceFactory { @Override public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException { String sourceName = name != null ? name : resource.getResource().getFilename(); if (sourceName != null &&(sourceName.endsWith(".yml") || sourceName.endsWith(".yaml"))) { Properties propertiesFromYaml = loadYml(resource); //將YML配置轉成Properties之后,再用PropertiesPropertySource綁定 return new PropertiesPropertySource(sourceName, propertiesFromYaml); } else { return super.createPropertySource(name, resource); } } //將YML格式的配置轉成Properties配置 private Properties loadYml(EncodedResource resource) throws IOException{ YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(resource.getResource()); factory.afterPropertiesSet(); return factory.getObject(); } } ~~~ * 然后基于上一節的代碼,在Family類的上面加上如下注解即可。通過factory屬性明確的指定使用我們自定義的MixPropertySourceFactory加載yml配置文件。 ~~~ @PropertySource(value = {"classpath:family.yml"}, factory = MixPropertySourceFactory.class) public class Family { ~~~ ## 三、使用@ImportResource加載Spring的xml配置文件 在沒有Spring注解的時代,spring的相關配置都是通過xml來完成的,如:beans.xml。下面的XML配置的含義是:將com.zimug.bootlaunch.service.TestBeanService實例化并注入到Spring上下文環境中。 ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="testBeanService" class="com.zimug.bootlaunch.service.TestBeanService"></bean> </beans> ~~~ * 新建一個空類,com.zimug.bootlaunch.service.TestBeanService。 * 測試用例,測試Spring上下文環境中是否有testBeanService這樣一個bean,有的話表示xml配置文件已經生效,成功將testBeanService實例化并注入到Spring上下文環境中: ~~~ @RunWith(SpringRunner.class) @SpringBootTest public class ImportResourceTests { @Autowired private ConfigurableApplicationContext ioc; @Test public void testHelloService() { //測試Spring上下文環境中是否有testBeanService這樣一個bean,有的話表示xml配置文件生效 boolean testBeanService= ioc.containsBean("testBeanService"); System.out.println(testBeanService); } } ~~~ * 因為還沒使用@ImportResource加載beans.xml,此時執行測試用例,輸出false表示beans.xml配置文件并未加載,所以沒有testBeanService的存在 * 在spring boot應用入口啟動類BootLauchApplication上加`@ImportResource(locations = {"classpath:beans.xml"})`,該注解用來加載Spring XML配置文件。 此時再試一下測試用例,輸出:true。表示beans.xml配置文件被正確加載。
                  <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>

                              哎呀哎呀视频在线观看