How to group properties with Spring Boot Configuration Properties

非 Y 不嫁゛ 提交于 2021-02-19 06:45:19

问题


According to Spring Boot documentation, properties can be grouped and a property may appear in more than one group. 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. 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?

--- 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. 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. 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


回答1:


You can compose them with help of inner classes:

Property file

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

Java/Spring mapping:

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. 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.




回答2:


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.

@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

}


来源:https://stackoverflow.com/questions/34059989/how-to-group-properties-with-spring-boot-configuration-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!