如何使用Spring Boot配置属性对属性进行分组 [英] How to group properties with Spring Boot Configuration Properties

查看:134
本文介绍了如何使用Spring Boot配置属性对属性进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

推荐答案

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

You can compose them with help of inner classes:

属性文件

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

Java/Spring映射:

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

我们成功使用了这种方法,因为Java组成模仿了属性文件的结构.属性也是有效的,因此如果配置不正确,您可能会快速失败.

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.

这篇关于如何使用Spring Boot配置属性对属性进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆