<tfoot id='kBMG1'></tfoot>
  • <small id='kBMG1'></small><noframes id='kBMG1'>

      <bdo id='kBMG1'></bdo><ul id='kBMG1'></ul>
  • <i id='kBMG1'><tr id='kBMG1'><dt id='kBMG1'><q id='kBMG1'><span id='kBMG1'><b id='kBMG1'><form id='kBMG1'><ins id='kBMG1'></ins><ul id='kBMG1'></ul><sub id='kBMG1'></sub></form><legend id='kBMG1'></legend><bdo id='kBMG1'><pre id='kBMG1'><center id='kBMG1'></center></pre></bdo></b><th id='kBMG1'></th></span></q></dt></tr></i><div id='kBMG1'><tfoot id='kBMG1'></tfoot><dl id='kBMG1'><fieldset id='kBMG1'></fieldset></dl></div>

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

        如何使用@Value Spring Annotation 注入 Map?

        时间:2023-07-26

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

                <tbody id='SGHzZ'></tbody>
                <bdo id='SGHzZ'></bdo><ul id='SGHzZ'></ul>

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

                  <i id='SGHzZ'><tr id='SGHzZ'><dt id='SGHzZ'><q id='SGHzZ'><span id='SGHzZ'><b id='SGHzZ'><form id='SGHzZ'><ins id='SGHzZ'></ins><ul id='SGHzZ'></ul><sub id='SGHzZ'></sub></form><legend id='SGHzZ'></legend><bdo id='SGHzZ'><pre id='SGHzZ'><center id='SGHzZ'></center></pre></bdo></b><th id='SGHzZ'></th></span></q></dt></tr></i><div id='SGHzZ'><tfoot id='SGHzZ'></tfoot><dl id='SGHzZ'><fieldset id='SGHzZ'></fieldset></dl></div>

                  本文介绍了如何使用@Value Spring Annotation 注入 Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在 Spring 中使用 @Value 注释从属性文件中将值注入 Map?

                  How can I inject values into a Map from the properties file using the @Value annotation in Spring?

                  我的 Spring Java 类是,我尝试使用 $,但收到以下错误消息:

                  My Spring Java class is and I tried using the $, but got the following error message:

                  无法自动装配字段:私有 java.util.Map Test.standard;嵌套异常是 java.lang.IllegalArgumentException:无法解析字符串值${com.test.standard}"中的占位符com.test.standard";

                  Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.test.standard' in string value "${com.test.standard}"

                  @ConfigurationProperty("com.hello.foo")
                  public class Test {
                  
                     @Value("${com.test.standard}")
                     private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>
                  
                     private String enabled;
                  
                  }
                  

                  我在 .properties 文件中有以下属性

                  I have the following properties in a .properties file

                  com.test.standard.name1=Pattern1
                  com.test.standard.name2=Pattern2
                  com.test.standard.name3=Pattern3
                  com.hello.foo.enabled=true
                  

                  推荐答案

                  我相信 Spring Boot 支持通过 @ConfigurationProperties 注释.

                  I believe Spring Boot supports loading properties maps out of the box with @ConfigurationProperties annotation.

                  根据该文档,您可以加载属性:

                  According that docs you can load properties:

                  my.servers[0]=dev.bar.com
                  my.servers[1]=foo.bar.com
                  

                  像这样变成豆子:

                  @ConfigurationProperties(prefix="my")
                  public class Config {
                  
                      private List<String> servers = new ArrayList<String>();
                  
                      public List<String> getServers() {
                          return this.servers;
                      }
                  }
                  

                  我之前使用过@ConfigurationProperties 功能,但没有加载到地图中.您需要使用 @EnableConfigurationProperties 注释 以启用此功能.

                  I used @ConfigurationProperties feature before, but without loading into map. You need to use @EnableConfigurationProperties annotation to enable this feature.

                  这个功能很酷的地方在于你可以验证你的属性.

                  Cool stuff about this feature is that you can validate your properties.

                  这篇关于如何使用@Value Spring Annotation 注入 Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 JPA 注释注释 MYSQL 自动增量字段 下一篇:Java 中如何以及在何处使用注解?

                  相关文章

                  最新文章

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

                2. <small id='puHwo'></small><noframes id='puHwo'>

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

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