Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将属性与Spring引导配置属性分组_Java_Spring_Properties_Configuration_Spring Boot - Fatal编程技术网

Java 如何将属性与Spring引导配置属性分组

Java 如何将属性与Spring引导配置属性分组,java,spring,properties,configuration,spring-boot,Java,Spring,Properties,Configuration,Spring Boot,根据Spring引导文档,属性可以分组,并且一个属性可以出现在多个组中。但在我们创建一个标有@ConfigurationProperties(prefix=“test1”)的属性类时,组名将是前缀test1。现在,如果我有另一个属性类,比如前缀为“test2”,我怎么能说后一个属性类有来自组test1的属性呢 ---更新--- 添加了嵌套类,但它不工作 @Configuration @Profile({"wmx"}) @EnableConfigurationProperties @Configu

根据Spring引导文档,属性可以分组,并且一个属性可以出现在多个组中。但在我们创建一个标有@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将不会加载嵌套类中的属性

属性文件

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组合模拟了属性文件的结构。属性也是可验证的,所以如果配置不正确,您可能会很快失败

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


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

注释处理器会自动将内部类视为嵌套属性。确保定义了getter和setter

@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

}

我不确定你们有什么房产。请举例说明您的属性,以及迄今为止您在ConfigurationProperties方面做了哪些尝试?谢谢。这很好,但是如果Test2属性与Test1没有直接关系呢。因此,出于文档的原因,我们希望对它们进行分组。例如,RabbitMQ配置的一组属性和回调机制的另一组属性。现在,每个集合都有一个url属性,希望将它们分组到环境属性中。我们想用这种格式为我们的客户端生成文档好奇的是,你能在编译时验证属性吗?我已经添加了SpringBootMaven插件,但它不验证属性,并且在部署时进行验证。在编译时验证属性对我来说没有意义。如果您的属性在编译后不会更改,那么为什么要将它们外部化呢?许多现代云环境强烈建议这样做:。这就是编译时验证没有意义的原因。