在Spring中从属性文件注入值数组(不使用Spring Boot)

时间:2019-01-09 23:14:05

标签: java spring spring-boot

要求与此处Injecting array of values from properties file in spring boot所发布的问题相同,即使用了application.properties文件中定义的相关属性的列表。

问题是,我的代码使用Core Spring。我如何实现相同的目的而不必为了使用@ConfigurationProperties而引入Spring Boot?使用逗号分隔的值列表从长远来看是行不通的,因为我希望表示配置对象列表,而不仅仅是字符串列表

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用Apache Commons Configuration进行查看,它提供了丰富的属性处理方法。

如果要坚持使用属性文件,可以考虑使用this page作为列表和数组:-

List<Object> colorList = config.getList("colors.pie");

或者您可以使用基于XML的hierarchical configurations并使用以下内容:-

List<HierarchicalConfiguration<ImmutableNode>> fields = config.configurationsAt("tables.table(0).fields.field");

List<Object> fieldNames = config.getList("tables.table(0).fields.field.name");

阅读他们的user-guide,您应该找到想要的东西。

答案 1 :(得分:0)

如果可以将spring-boot jar作为依赖项添加,则可以将@ConfigurationProperties与核心spring一起使用。

package com.stackoverflow.q54119803;

import static java.util.stream.Collectors.*;
import static org.junit.Assert.*;

import java.util.List;
import java.util.stream.Stream;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;

@SuppressWarnings("javadoc")
public class So54119803 {

    /** The Constant SPRING_CLASS_RULE. */
    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    final String anotherPropValue = "anotherPropValue";
    final List<String> expected = Stream.of("string1", this.anotherPropValue)
        .collect(toList());

    /** The spring method rule. */
    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Autowired
    Props props;

    @Test
    public void test() {

        System.out.println(this.props);

        assertEquals(this.anotherPropValue, this.props.getAnotherProp());
        assertEquals(String.class, this.props.getClazz());
        assertEquals(this.expected, this.props.getStrings());

    }

    @Configuration
    @EnableConfigurationProperties
    static class Config {

        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

            final ClassPathResource location = new ClassPathResource("props.properties");
            final PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();

            propertySourcesPlaceholderConfigurer.setLocation(location);
            return propertySourcesPlaceholderConfigurer;
        }

        @Bean
        @ConfigurationProperties("test")
        Props props() {
            return new Props();
        }

    }

    static class Props {

        String anotherProp;

        List<String> strings;

        Class<?> clazz;

        public String getAnotherProp() {
            return this.anotherProp;
        }

        public void setAnotherProp(String anotherProp) {
            this.anotherProp = anotherProp;
        }

        public List<String> getStrings() {
            return this.strings;
        }

        public void setStrings(List<String> strings) {
            this.strings = strings;
        }

        public Class<?> getClazz() {
            return this.clazz;
        }

        public void setClazz(Class<?> clazz) {
            this.clazz = clazz;
        }

        @Override
        public String toString() {
            return "Props [anotherProp=" + this.anotherProp + ", strings=" + this.strings + ", clazz=" + this.clazz
                    + "]";
        }

    }
}

道具文件示例:props.properties

#Conversions and references work fine
test.anotherProp=anotherPropValue
test.strings=string1,${test.anotherProp}
test.clazz=java.lang.String

示例中的依赖项:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot</artifactId>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <scope>test</scope>
</dependency>