简体   繁体   English

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

[英]How to group properties with Spring Boot Configuration Properties

According to Spring Boot documentation, properties can be grouped and a property may appear in more than one group. 根据Spring Boot文档,属性可以分组,并且一个属性可以出现在多个组中。 But at the moment when we create a property class marked with @ConfigurationProperties(prefix="test1") the group name will be the prefix which is test1. 但是,当我们创建一个标有@ConfigurationProperties(prefix =“ test1”)的属性类时,组名将是前缀test1。 Now if I have another property class for example with prefix as "test2" how can I say this latter one has a property from the group test1? 现在,如果我有另一个以“ test2”为前缀的属性类,那么我该如何说后者具有来自test1组的一个属性呢?

--- UPDATE --- Added nested class but it's not working ---更新---添加了嵌套类,但无法正常工作

@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;
    }
}

The commented annotation @ConfigurationProperties on the inner class is done after failing my tests. 在我的测试失败之后,完成对内部类的注释@ConfigurationProperties注释。 Spring doesn't load those properties with or without the annotation unless they are in the same property file, in this case application-emx.properties. Spring不会加载带有或不带有注释的那些属性,除非它们位于同一属性文件中,在本例中为application-emx.properties。 Why is that? 这是为什么? I want to separate these properties 我想分开这些属性

=== RESOLVED ==== I noticed that I had to add a field of type the nested class with getter/setter methods otherwise Spring won't load the properties in the nested class ===已解决====我注意到我必须使用getter / setter方法添加一个嵌套类类型的字段,否则Spring不会在嵌套类中加载属性

You can compose them with help of inner classes: 您可以在内部类的帮助下编写它们:

Property file 属性文件

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

Java/Spring mapping: 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;
    }
}

We had success with this approach, because java composition mimics structure of property file. 我们成功使用了这种方法,因为java组成模仿了属性文件的结构。 Also properties are validatable, so you can fail fast if configuration is not right. 属性也是有效的,因此如果配置不正确,您可能会快速失败。

Downside of this approach is that properties are mutable. 这种方法的缺点是属性是可变的。

If your properties file is getting too big, your application most probably has wider problems. 如果属性文件太大,则您的应用程序很可能会遇到更广泛的问题。

The annotation processor automatically considers inner classes as nested properties. 注释处理器自动将内部类视为嵌套属性。 Make sure you have getters and setters defined. 确保已定义吸气剂和吸气剂。

@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 !!!

    }

}

The same effect can be achieved with non-inner class but you should use the @NestedConfigurationProperty annotation on a field to indicate that a regular (non-inner) class should be treated as if it were nested. 使用非内部类可以实现相同的效果,但是您应该在字段上使用@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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM