如何使用Spring Boot配置属性对属性进行分组

时间:2015-12-03 07:20:18

标签: java spring properties configuration spring-boot

根据Spring Boot文档,可以对属性进行分组,并且属性可以出现在多个组中。但是当我们创建一个标有@ConfigurationProperties(prefix =" test1")的属性类时,组名将是test1的前缀。现在,如果我有另一个属性类,例如前缀为" test2"我怎么能说后者有一个来自组test1的属性?

---更新--- 添加了嵌套类,但它无法正常工作

@Configuration
@Profile({"wmx"})
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapp.wmx", locations = {"classpath:application-wmx.properties", "classpath:myapp-env.properties"})
public class WmxProperties {

    /**
     * The WMX implementation to be loaded.
     */
    @NotNull(message = "Must be configured.")
    private ProfileEnum profile;

    //@ConfigurationProperties(locations = "classpath:myapp-env.properties")
    public static class Env {
        /**
         * Host name for WMX.
         */
        private String host;
        /**
         * Port number for WMX.
         */
        //@Pattern(regexp = "^[1-9]\\d*$", message = "Positive port number only.")
        private Integer port;
        /**
         * Provider name.
         */
        @NotBlank
        private String providerName;

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public Integer getPort() {
            return port;
        }

        public void setPort(Integer port) {
            this.port = port;
        }

        public String getProviderName() {
            return providerName;
        }

        public void setProviderName(String providerName) {
            this.providerName = providerName;
        }
    }

    public ProfileEnum getProfile() {
        return profile;
    }

    public void setProfile(ProfileEnum profile) {
        this.profile = profile;
    }
}

内部类的注释注释@ConfigurationProperties在我的测试失败后完成。 Spring不会在有或没有注释的情况下加载这些属性,除非它们位于同一属性文件中,在本例中为application-emx.properties。这是为什么?我想分开这些属性

===已解决==== 我注意到我必须使用getter / setter方法添加嵌套类的字段,否则Spring不会在嵌套类中加载属性

2 个答案:

答案 0 :(得分:2)

你可以在内部类的帮助下编写它们:

属性文件

test1.property1=...
test1.test2.property2=...
test1.test2.property3=...

Java / Spring映射:

import javax.validation.constraints.NotNull;

import lombok.Getter;
import lombok.Setter;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:myapp.properties")
public class ApplicationProperties {

    private String property1;
    private Test2 test2;

    @Getter
    @Setter
    @ConfigurationProperties(prefix = "test2")
    public static class Test2 {
        @NotNull
        private String property2;
        @NotNull
        private String property3;
    }
}

我们用这种方法取得了成功,因为java组合模仿了属性文件的结构。属性也是可验证的,因此如果配置不正确,您可以快速失败。

这种方法的缺点是属性是可变的。

如果您的属性文件太大,您的应用程序很可能会有更广泛的问题。

答案 1 :(得分:0)

注释处理器自动将内部类视为嵌套属性。确保已定义吸气剂和吸气剂。

@ConfigurationProperties(prefix="server")
public class ServerProperties {

    private String name;

    private Host host;

    // ... getter and setters !!!

    public static class Host {

        private String ip;

        private int port;

        // ... getter and setters !!!

    }

}

使用非内部类可以实现相同的效果,但是您应该在字段上使用@NestedConfigurationProperty批注,以指示应该将常规(非内部)类视为嵌套类。

@ConfigurationProperties(prefix="server")
public class ServerProperties {

    private String name;

    @NestedConfigurationProperty
    private Host host;

    // ... getter and setters !!!

}

public class Host {

    private String ip;

    private int port;

    // ... getter and setters

}