我想在.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:如果可能的话,我想这样做没有任何自定义代码。


当前回答

如果你使用的是最新的Spring框架版本(我相信是Spring 3.1+),你不需要在SpringEL中进行字符串分割,

简单地添加PropertySourcesPlaceholderConfigurer和DefaultConversionService在你的Spring的配置类(一个与配置注释),例如:

@Configuration
public class AppConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean public ConversionService conversionService() {
        return new DefaultConversionService();
    }
}

在你的课堂上

@Value("${list}")
private List<String> list;

在属性文件中

list=A,B,C,D,E

如果没有DefaultConversionService,当您将值注入字段时,您只能将逗号分隔的字符串放入字符串数组中,但是DefaultConversionService为您做了一些方便的魔术,并将它们添加到Collection, array等(如果您想了解更多,请检查实现)。

有了这两个,它甚至可以处理包括换行符在内的所有多余的空白,因此您不需要添加额外的逻辑来修剪它们。

其他回答

如果你使用的是最新的Spring框架版本(我相信是Spring 3.1+),你不需要在SpringEL中进行字符串分割,

简单地添加PropertySourcesPlaceholderConfigurer和DefaultConversionService在你的Spring的配置类(一个与配置注释),例如:

@Configuration
public class AppConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean public ConversionService conversionService() {
        return new DefaultConversionService();
    }
}

在你的课堂上

@Value("${list}")
private List<String> list;

在属性文件中

list=A,B,C,D,E

如果没有DefaultConversionService,当您将值注入字段时,您只能将逗号分隔的字符串放入字符串数组中,但是DefaultConversionService为您做了一些方便的魔术,并将它们添加到Collection, array等(如果您想了解更多,请检查实现)。

有了这两个,它甚至可以处理包括换行符在内的所有多余的空白,因此您不需要添加额外的逻辑来修剪它们。

如果您正在阅读这篇文章,并且正在使用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

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

 @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

你是否考虑过@Autowireding构造函数或在body中使用setter和String.split() ?

class MyClass {
    private List<String> myList;

    @Autowired
    public MyClass(@Value("${my.list.of.strings}") final String strs) {
        myList = Arrays.asList(strs.split(","));
    }

    //or

    @Autowired
    public void setMyList(@Value("${my.list.of.strings}") final String strs) {
        myList = Arrays.asList(strs.split(","));
    }
}

我倾向于用这些方法中的一种来进行自动装配,以增强代码的可测试性。

通过指定my.list.of。字符串=ABC,CDE,EFG在.properties文件和使用

@ value (" $ {my.list.of.strings} ") private String[] myString;

你可以得到字符串的数组。并使用CollectionUtils。addAll(myList, myString),你可以得到字符串列表。