【问题标题】:Injecting array of values from properties file in spring (without using spring boot)在spring中从属性文件中注入值数组(不使用spring boot)
【发布时间】:2019-06-04 19:10:03
【问题描述】:

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

问题是,我的代码使用 Core Spring。如何在不引入 Spring Boot 以使用 @ConfigurationProperties 的情况下实现相同的目标?从长远来看,使用逗号分隔的值列表将不起作用,因为我希望表达配置对象列表,而不仅仅是字符串列表

谢谢

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您可以查看使用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,你应该会找到你要找的东西。

    【讨论】:

    • 谢谢,我希望找到一种 Spring 方式来完成此任务,而无需引入第三方库。
    【解决方案2】:

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

    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>
    

    【讨论】:

    • 感谢您的全面回答,但我希望避免包括 Spring Boot
    • 我不确定您是不想在 Spring Boot 上下文中还是根本不想将 jar 作为依赖项。
    • 我很快就会有非启动版本。
    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 2015-12-26
    • 2017-03-18
    • 1970-01-01
    • 2017-08-23
    • 2018-03-01
    • 2018-10-27
    • 2017-11-06
    相关资源
    最近更新 更多