【问题标题】:Spring, property value when having separators [duplicate]弹簧,具有分隔符时的属性值[重复]
【发布时间】:2013-03-14 17:12:54
【问题描述】:


我正在加载一个属性文件

@PropertySource("classpath:propFile.properties")

在此属性文件中,我有以下条目:

list.of.stg=a,b,c

此外,我愿意:

@Value("${list.of.stg}")public void setSomeList(...)<br>{in the method, the parameter has only the value a!!!}

您能否告诉我一种达到完整价值观的方法

list.of.stg
谢谢!

【问题讨论】:

    标签: java spring properties inject


    【解决方案1】:

    试试

    @Value("#{T(org.springframework.util.StringUtils).commaDelimitedListToStringArray(environment['list.of.stg'])}")
    

    【讨论】:

      【解决方案2】:

      由于属性值作为String 传入,您必须使用String#split,但您可以与Spring EL 结合使用:

      @Value("#{'${list.of.stg}'.split(',')}") 
      public void setSomeList(List<String> list) {
         this.myList = list;
      }
      

      或者只是在类成员变量上

      @Value("#{'${list.of.stg}'.split(',')}") 
      private List<String> myList;
      

      在使用@PropertySource 注解时,不要忘记创建PropertyPlaceholderConfigurer @Bean 以加载必要的属性文件。

      相关:@Value and ArrayList

      【讨论】:

      • 感谢您的回答 - 确实有效。但是,该网站只允许我“勾选”一个答案。我选择了 Jose 给出的那个,因为你已经有很多积分了:)对不起!:0
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2011-10-23
      • 2018-08-31
      • 1970-01-01
      相关资源
      最近更新 更多