我想在.properties文件中有一个值列表,即:

my.list.of.strings=ABC,CDE,EFG

并直接在我的类中加载它,即:

@Value("${my.list.of.strings}")
private List<String> myList;

据我所知,另一种方法是将它放在spring配置文件中,并将其作为bean引用加载(如果我错了请纠正我),即

<bean name="list">
 <list>
  <value>ABC</value>
  <value>CDE</value>
  <value>EFG</value>
 </list>
</bean>

但是有没有办法做到这一点呢?使用.properties文件? ps:如果可能的话,我想这样做没有任何自定义代码。


当前回答

考虑使用公共配置。它有内置的功能,以打破一个条目在属性文件数组/列表。结合SpEL和@Value应该会给你想要的


按照要求,这是你需要的(没有真正尝试过代码,可能会有一些错误,请原谅我):

在Apache Commons Configuration中,有PropertiesConfiguration。它支持将分隔字符串转换为数组/列表的特性。

例如,如果您有一个属性文件

#Foo.properties
foo=bar1, bar2, bar3

用下面的代码:

PropertiesConfiguration config = new PropertiesConfiguration("Foo.properties");
String[] values = config.getStringArray("foo");

会给你一个字符串数组["bar1", "bar2", "bar3"]

要和Spring一起使用,在你的app context xml中有这个:

<bean id="fooConfig" class="org.apache.commons.configuration.PropertiesConfiguration">
    <constructor-arg type="java.lang.String" value="classpath:/Foo.properties"/>
</bean>

在你的春豆里加入这个:

public class SomeBean {

    @Value("fooConfig.getStringArray('foo')")
    private String[] fooArray;
}

我相信这是可行的:P

其他回答

使用Spring EL:

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

假设你的属性文件正确加载如下:

my.list.of.strings=ABC,CDE,EFG

在我的情况下,一个整数列表的工作:

@Value("#{${my.list.of.integers}}")
private List<Integer> listOfIntegers;

属性文件:

my.list.of.integers={100,200,300,400,999}

您可以使用这样的注释来实现这一点

 @Value("#{T(java.util.Arrays).asList('${my.list.of.strings:a,b,c}')}") 
    private List<String> mylist;

这里my.list.of.strings将从属性文件中选择,如果它不在那里,那么将使用默认的a,b,c

在属性文件中,你可以有这样的东西

my.list.of.strings = d, e, f

如果使用属性占位符,则ser1702544示例将变成

@Value("#{myConfigProperties['myproperty'].trim().replaceAll(\"\\s*(?=,)|(?<=,)\\s*\", \"\").split(',')}") 

使用占位符xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
    <property name="properties" ref="myConfigProperties" />
    <property name="placeholderPrefix"><value>$myConfigProperties{</value></property>
</bean>    

<bean id="myConfigProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
     <property name="locations">
         <list>
                <value>classpath:myprops.properties</value>
         </list>
     </property>
</bean> 

如果您正在阅读这篇文章,并且正在使用Spring Boot,那么对于这个特性,您还有另外一个选项

通常逗号分隔的列表在现实世界中是非常笨拙的 (有时甚至是不可行的,如果你想在你的配置中使用逗号):

email.sendTo=somebody@example.com,somebody2@example.com,somebody3@example.com,.....

使用Spring Boot,你可以这样写(索引从0开始):

email.sendTo[0]=somebody@example.com
email.sendTo[1]=somebody2@example.com
email.sendTo[2]=somebody3@example.com

像这样使用它:

@Component
@ConfigurationProperties("email")
public class EmailProperties {

    private List<String> sendTo;

    public List<String> getSendTo() {
        return sendTo;
    }

    public void setSendTo(List<String> sendTo) {
        this.sendTo = sendTo;
    }

}


@Component
public class EmailModel {

  @Autowired
  private EmailProperties emailProperties;

  //Use the sendTo List by 
  //emailProperties.getSendTo()

}



@Configuration
public class YourConfiguration {
    @Bean
  public EmailProperties emailProperties(){
        return new EmailProperties();
  }

}


#Put this in src/main/resource/META-INF/spring.factories
  org.springframework.boot.autoconfigure.EnableAutoConfiguration=example.compackage.YourConfiguration