pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <bdo id='QbCDd'></bdo><ul id='QbCDd'></ul>

<small id='QbCDd'></small><noframes id='QbCDd'>

  • <legend id='QbCDd'><style id='QbCDd'><dir id='QbCDd'><q id='QbCDd'></q></dir></style></legend>
    <tfoot id='QbCDd'></tfoot>

    1. <i id='QbCDd'><tr id='QbCDd'><dt id='QbCDd'><q id='QbCDd'><span id='QbCDd'><b id='QbCDd'><form id='QbCDd'><ins id='QbCDd'></ins><ul id='QbCDd'></ul><sub id='QbCDd'></sub></form><legend id='QbCDd'></legend><bdo id='QbCDd'><pre id='QbCDd'><center id='QbCDd'></center></pre></bdo></b><th id='QbCDd'></th></span></q></dt></tr></i><div class="jdbfbv1" id='QbCDd'><tfoot id='QbCDd'></tfoot><dl id='QbCDd'><fieldset id='QbCDd'></fieldset></dl></div>

      1. Spring Boot 中的多態配置屬性

        Polymorphic configuration properties in Spring Boot(Spring Boot 中的多態配置屬性)
          <tbody id='mu5Fe'></tbody>

          <legend id='mu5Fe'><style id='mu5Fe'><dir id='mu5Fe'><q id='mu5Fe'></q></dir></style></legend>
            <bdo id='mu5Fe'></bdo><ul id='mu5Fe'></ul>
          • <i id='mu5Fe'><tr id='mu5Fe'><dt id='mu5Fe'><q id='mu5Fe'><span id='mu5Fe'><b id='mu5Fe'><form id='mu5Fe'><ins id='mu5Fe'></ins><ul id='mu5Fe'></ul><sub id='mu5Fe'></sub></form><legend id='mu5Fe'></legend><bdo id='mu5Fe'><pre id='mu5Fe'><center id='mu5Fe'></center></pre></bdo></b><th id='mu5Fe'></th></span></q></dt></tr></i><div class="thbhrln" id='mu5Fe'><tfoot id='mu5Fe'></tfoot><dl id='mu5Fe'><fieldset id='mu5Fe'></fieldset></dl></div>

            <small id='mu5Fe'></small><noframes id='mu5Fe'>

          • <tfoot id='mu5Fe'></tfoot>

                1. 本文介紹了Spring Boot 中的多態配置屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想在 Spring 上使用多態配置屬性,使用 Spring 的 @ConfigurationProperties 注釋.

                  I would like to use polymorphic configuration properties on Spring, using Spring's @ConfigurationProperties annotation.

                  假設我們有以下 POJO 類.

                  Suppose we have the following POJO classes.

                  public class Base {
                    private String sharedProperty;
                  
                    public String getSharedProperty() {
                      return sharedProperty;
                    }
                  
                    public String setSharedProperty(String sharedProperty) {
                      this.sharedProperty = sharedProperty;
                    }
                  }
                  
                  public class Foo extends Base {
                    private String fooProperty;
                  
                    public String getFooProperty() {
                      return fooProperty;
                    }
                  
                    public String setFooProperty(String sharedProperty) {
                      this. fooProperty = fooProperty;
                    }
                  }
                  
                  public class Bar extends Base {
                    private String barProperty;
                  
                    public String getSharedProperty() {
                      return sharedProperty;
                    }
                  
                    public String setBarProperty(String barProperty) {
                      this.barProperty = barProperty;
                    }
                  }
                  

                  還有配置屬性類,

                  @Component
                  @ConfigurationProperties(prefix = "playground")
                  public class SomeConfigurationProperties {
                    private List<Base> mixed;
                  
                    public List<Base> getMixed() {
                      return mixed;
                    }
                  
                    public void setMixed(List<Base> mixed) {
                      this.mixed = mixed;
                    }
                  }
                  

                  還有application.yml文件,

                  playground:
                    mixed:
                      - shared-property: "shared prop"
                        foo-property: "foo prop"
                      - shared-property: "shared prop"
                        bar-property: "bar prop"
                  

                  但是,使用此配置,Spring 使用 Base 對象列表而不是它們的子類來初始化帶有 @ConfigurationProperties 注釋的類.這實際上是一種預期行為(出于安全考慮).

                  However, with this configuration, Spring initializes the @ConfigurationProperties-annotated class with the list of Base objects, instead of their subclasses. That is, actually, an expected behavior (due to security concerns).

                  有沒有辦法強制 SnakeYAML 的行為使用子類,或實現任何類型的自定義反序列化提供程序?

                  Is there a way to enforce the behavior of SnakeYAML to use subclasses, or implement any kind of custom deserialization provider?

                  推薦答案

                  雖然可以實現自定義 PropertySources 和/或 ConversionService,不需要自定義反序列化提供程序.

                  Although it is possible to implement custom PropertySources and/or ConversionService, a custom deserialization provider is not necessary.

                  Spring 將相同的屬性綁定到多個 bean 沒有問題.您的實現無法正常工作的原因是您只在 ApplicationContext 中注冊了一個 bean,并在基類上使用了 @Component 注釋.這告訴組件掃描器只有一個 Base 類型的單例.因為 FooBar 沒有注冊為 bean,所以它們不會被綁定.

                  Spring has no issues binding the same properties to multiple beans. The reason your implementation is not working is because you are only registering one bean with the ApplicationContext with the @Component annotation on the base class. This is telling the component scanner that there is only one singleton of type Base. Because Foo and Bar are not registered as beans, they won't be bound to.

                  如果您考慮使這些多態的唯一原因是在基于 SnakeYAML 的配置中共享屬性名稱前綴,那么您實際上不需要引入多態關系,并且可以綁定到共享屬性通過不同類中的通用字段名稱.

                  If the only reason you are looking at making these polymorphic is to share property name prefixes in SnakeYAML based config, then you actually do not need to introduce the polymorphic relationship, and can bind to shared properties by a common field name in different classes.

                  有很多方法可以實現你所要求的,但是以多態的方式,這里有一些最直接的簡單方法:

                  There are many ways to implement what you are asking for however in a polymorphic way, here are a few of the most straight forward simple ones:

                  不要將 @ConfigurationProperties@Component 注釋應用于基類,而是將它們應用于具有相同屬性名稱前綴的具體類.這不是我的首選方法,因為每個 bean 都不會以設置它們的屬性為條件,但它可能適合您的需要.根據您的 Spring 配置是否允許重新加載屬性,Spring 將維護所有 bean 上的綁定.

                  Instead of applying the @ConfigurationProperties and @Component annotations on the base class, apply them on the concrete classes, with the same property name prefix. This wouldn't be my preferred approach, as each bean would not be conditional on their properties being set, however it may suit your needs. Depending on if your Spring Configuration allows properties to be reloaded, Spring will maintain the bindings on all of the beans.

                  注意:從 IntelliJ Idea 2018.3 開始,添加了檢查配置文件以將重復的前綴鍵識別為錯誤.您可能想忽略這一點,或禁止顯示警告.

                  我成功測試了以下內容:

                  I tested the following successfully:

                  Base.java

                  package sample;
                  
                  public class Base {
                      private String sharedProperty;
                  
                      public String getSharedProperty() {
                          return sharedProperty;
                      }
                  
                      public void setSharedProperty(String sharedProperty) {
                          this.sharedProperty = sharedProperty;
                      }
                  }
                  

                  Foo.java

                  package sample;
                  
                  import org.springframework.boot.context.properties.ConfigurationProperties;
                  import org.springframework.stereotype.Component;
                  
                  @Component
                  @ConfigurationProperties("playground")
                  public class Foo extends Base {
                      private String fooProperty;
                  
                      public String getFooProperty() {
                          return fooProperty;
                      }
                  
                      public void setFooProperty(String fooProperty) {
                          this.fooProperty = fooProperty;
                      }
                  }
                  

                  Bar.java

                  package sample;
                  
                  import org.springframework.boot.context.properties.ConfigurationProperties;
                  import org.springframework.stereotype.Component;
                  
                  @Component
                  @ConfigurationProperties("playground")
                  public class Bar extends Base {
                      private String barProperty;
                  
                      public String getBarProperty() {
                          return barProperty;
                      }
                  
                      public void setBarProperty(String barProperty) {
                          this.barProperty = barProperty;
                      }
                  }
                  

                  application.yml

                  application.yml

                  playground:
                    shared-property: "shared prop"
                    foo-property: "foo prop"
                    bar-property: "bar prop"
                  

                  SampleAppTest.java

                  SampleAppTest.java

                  package sample;
                  
                  import org.junit.jupiter.api.Test;
                  import org.springframework.beans.factory.annotation.Autowired;
                  import org.springframework.boot.test.context.SpringBootTest;
                  import org.springframework.core.env.Environment;
                  
                  import java.util.List;
                  
                  import static org.junit.jupiter.api.Assertions.assertEquals;
                  
                  @SpringBootTest
                  public class SampleAppTest {
                  
                      @Autowired
                      public Environment environment;
                  
                      @Test
                      public void test(@Autowired Bar bar, @Autowired Foo foo) {
                          assertEquals("shared prop", bar.getSharedProperty());
                          assertEquals("shared prop", foo.getSharedProperty());
                          assertEquals("bar prop", bar.getBarProperty());
                          assertEquals("foo prop", foo.getFooProperty());
                      }
                  
                      @Test
                      public void testSuper(@Autowired List<Base> props) {
                          assertEquals(2, props.size());
                      }
                  }
                  

                  以屬性為條件的多態 ConfigurationProperties bean

                  如果缺少特定屬性,您可能不希望實例化某些具體實現.此外,您可能不想將 @ConfigurationProperties@Component 注釋耦合到每個具體類.此實現通過 Spring @Configuration bean 構造 ConfigurationProperties bean.配置 bean 確保它們僅通過屬性存在檢查有條件地構造.如果沒有其他 Base bean 滿足條件并且共享屬性存在,則此實現還會創建具體類型 Base 的 bean.此處使用與上一個示例相同的單元測試并通過:

                  Polymorphic ConfigurationProperties beans conditional on properties

                  You may not want certain concrete implementations to be instantiated if their specific properties are missing. Furthermore, you may not want to couple the @ConfigurationProperties and @Component annotations to each concrete class. This implementation constructs the ConfigurationProperties beans via a Spring @Configuration bean. The configuration bean ensures they are only constructed conditionally via a property existence check. This implementation also creates a bean of concrete type Base if none of the other Base beans meet conditions and the shared properties exist. The same unit test from the previous example is used here and passes:

                  Base.java

                  package sample;
                  
                  public class Base {
                      private String sharedProperty;
                  
                      public String getSharedProperty() {
                          return sharedProperty;
                      }
                  
                      public void setSharedProperty(String sharedProperty) {
                          this.sharedProperty = sharedProperty;
                      }
                  }
                  

                  Foo.java

                  package sample;
                  
                  public class Foo extends Base {
                      private String fooProperty;
                  
                      public String getFooProperty() {
                          return fooProperty;
                      }
                  
                      public void setFooProperty(String fooProperty) {
                          this.fooProperty = fooProperty;
                      }
                  }
                  

                  Bar.java

                  package sample;
                  
                  public class Bar extends Base {
                      private String barProperty;
                  
                      public String getBarProperty() {
                          return barProperty;
                      }
                  
                      public void setBarProperty(String barProperty) {
                          this.barProperty = barProperty;
                      }
                  }
                  

                  SampleConfiguration.java

                  SampleConfiguration.java

                  package sample;
                  
                  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
                  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
                  import org.springframework.boot.context.properties.ConfigurationProperties;
                  import org.springframework.context.annotation.Bean;
                  import org.springframework.context.annotation.Configuration;
                  
                  @Configuration
                  public class SampleConfiguration {
                  
                      @Bean
                      @ConfigurationProperties("playground")
                      @ConditionalOnProperty("playground.foo-property")
                      public Foo foo() {
                          return new Foo();
                      }
                  
                      @Bean
                      @ConfigurationProperties("playground")
                      @ConditionalOnProperty("playground.bar-property")
                      public Bar bar() {
                          return new Bar();
                      }
                  
                      @Bean
                      @ConfigurationProperties("playground")
                      @ConditionalOnProperty("playground.shared-property")
                      @ConditionalOnMissingBean(Base.class)
                      public Base base() {
                          return new Base();
                      }
                  }
                  

                  application.yml

                  application.yml

                  playground:
                    shared-property: "shared prop"
                    foo-property: "foo prop"
                    bar-property: "bar prop"
                  

                  SampleAppTest.java

                  SampleAppTest.java

                  package sample;
                  
                  import org.junit.jupiter.api.Test;
                  import org.springframework.beans.factory.annotation.Autowired;
                  import org.springframework.boot.test.context.SpringBootTest;
                  import org.springframework.core.env.Environment;
                  
                  import java.util.List;
                  
                  import static org.junit.jupiter.api.Assertions.assertEquals;
                  
                  @SpringBootTest
                  public class SampleAppTest {
                  
                      @Autowired
                      public Environment environment;
                  
                      @Test
                      public void test(@Autowired Bar bar, @Autowired Foo foo) {
                          assertEquals("shared prop", bar.getSharedProperty());
                          assertEquals("shared prop", foo.getSharedProperty());
                          assertEquals("bar prop", bar.getBarProperty());
                          assertEquals("foo prop", foo.getFooProperty());
                      }
                  
                      @Test
                      public void testSuper(@Autowired List<Base> props) {
                          assertEquals(2, props.size());
                      }
                  }
                  

                  這篇關于Spring Boot 中的多態配置屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)

                  1. <legend id='QRlXM'><style id='QRlXM'><dir id='QRlXM'><q id='QRlXM'></q></dir></style></legend>

                    <small id='QRlXM'></small><noframes id='QRlXM'>

                      <tbody id='QRlXM'></tbody>

                            <bdo id='QRlXM'></bdo><ul id='QRlXM'></ul>
                          • <tfoot id='QRlXM'></tfoot>
                          • <i id='QRlXM'><tr id='QRlXM'><dt id='QRlXM'><q id='QRlXM'><span id='QRlXM'><b id='QRlXM'><form id='QRlXM'><ins id='QRlXM'></ins><ul id='QRlXM'></ul><sub id='QRlXM'></sub></form><legend id='QRlXM'></legend><bdo id='QRlXM'><pre id='QRlXM'><center id='QRlXM'></center></pre></bdo></b><th id='QRlXM'></th></span></q></dt></tr></i><div class="5rnhrvt" id='QRlXM'><tfoot id='QRlXM'></tfoot><dl id='QRlXM'><fieldset id='QRlXM'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 包装机_厂家_价格-山东包装机有限公司| 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 合肥升降机-合肥升降货梯-安徽升降平台「厂家直销」-安徽鼎升自动化科技有限公司 | 塑木弯曲试验机_铜带拉伸强度试验机_拉压力测试台-倾技百科 | 沙盘模型公司_沙盘模型制作公司_建筑模型公司_工业机械模型制作厂家 | 3dmax渲染-效果图渲染-影视动画渲染-北京快渲科技有限公司 | 高速龙门架厂家_监控杆_多功能灯杆_信号灯杆_锂电池太阳能路灯-鑫世源照明 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 四川实木门_成都实木门 - 蓬溪聚成门业有限公司 | 橡胶接头_橡胶软接头_可曲挠橡胶接头-巩义市创伟机械制造有限公司 | 直齿驱动-新型回转驱动和回转支承解决方案提供商-不二传动 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 警方提醒:赣州约炮论坛真的安全吗?2025年新手必看的网络交友防坑指南 | 太原装修公司_山西整装家装设计_太原室内装潢软装_肖邦家居 | 烘干设备-热泵烘干机_广东雄贵能源设备有限公司 | 西子馋火锅鸡加盟-太原市龙城酉鼎餐饮管理有限公司 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 礼至家居-全屋定制家具_一站式全屋整装_免费量房设计报价 | 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 江苏农村商业银行招聘网_2024江苏农商行考试指南_江苏农商行校园招聘 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 化工ERP软件_化工新材料ERP系统_化工新材料MES软件_MES系统-广东顺景软件科技有限公司 | 蒸汽吸附分析仪-进口水分活度仪|康宝百科 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 济南宣传册设计-画册设计_济南莫都品牌设计公司 | 网站建设-网站制作-网站设计-网站开发定制公司-网站SEO优化推广-咏熠软件 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 搪玻璃冷凝器_厂家-越宏化工设备 | 防腐储罐_塑料储罐_PE储罐厂家_淄博富邦滚塑防腐设备科技有限公司 | 工业雾炮机_超细雾炮_远程抑尘射雾器-世纪润德环保设备 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | 实体店商新零售|微赢|波后|波后合作|微赢集团 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 黄石东方妇产医院_黄石妇科医院哪家好_黄石无痛人流医院 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 |