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

     2023-02-26     219

关键词:

【中文标题】如何使用 Spring Boot 配置属性对属性进行分组【英文标题】:How to group properties with Spring Boot Configuration Properties 【发布时间】:2016-03-07 17:05:52 【问题描述】:

根据 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 不会加载嵌套类中的属性

【问题讨论】:

我不确定你有什么属性。您能否举例说明您的属性以及到目前为止您对 ConfigurationProperties 的尝试? 【参考方案1】:

您可以借助内部类来组合它们:

属性文件

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

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

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

【讨论】:

谢谢。这很好,但如果 Test2 属性与 Test1 没有直接关系怎么办。因此,出于文档原因,我们希望将它们分组。例如,一组用于 RabbitMQ 配置的属性和另一组用于回调机制的属性。现在每个集合都有一个 url 属性,希望它们被分组到环境属性中。我们希望使用这种格式为我们的客户生成文档 只是好奇,你能在编译时验证属性吗?我添加了 spring-boot maven 插件,但它不验证属性并且验证发生在部署时。 在编译时验证属性对我来说没有意义。如果你的属性在编译后不会改变,为什么要外部化它们呢? 很多现代云环境强烈推荐这个:12factor.net/config。这就是编译时验证没有意义的原因。【参考方案2】:

注解处理器自动将内部类视为嵌套属性。确保定义了 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


【讨论】:

如何使用 Spring Boot 和嵌入式 Tomcat 配置此属性?

】如何使用SpringBoot和嵌入式Tomcat配置此属性?【英文标题】:HowdoIconfigurethispropertywithSpringBootandanembeddedTomcat?【发布时间】:2015-10-0607:49:32【问题描述】:我是在application.properties文件中配置诸如connectionTimeout之类的属性,还是在... 查看详情

使用 spring-boot:run 时是不是可以使用 spring-boot 命令行属性?

...发布时间】:2015-02-0219:55:03【问题描述】:简短的问题:如何配置环境配置文件,甚至只是一个替代配置文 查看详情

如何根据 Spring Boot 中的活动配置文件设置注释属性值?

】如何根据SpringBoot中的活动配置文件设置注释属性值?【英文标题】:HowtosetannotationattributevaluebasedontheactiveprofileinSpringBoot?【发布时间】:2020-12-2310:24:49【问题描述】:我在SpringBoot应用程序中使用两个镜像数据库模式,并在启... 查看详情

Spring-boot 使用环境变量使用破折号/连字符配置属性

】Spring-boot使用环境变量使用破折号/连字符配置属性【英文标题】:Spring-bootConfiguringpropertieswithdash/hyphenusingenvironmentvariables【发布时间】:2022-01-1700:00:36【问题描述】:我最喜欢的spring-boot功能之一是能够使用unix环境变量配置大... 查看详情

在 Spring Boot 中配置 Jackson 以省略延迟加载属性

...02-1701:23:15【问题描述】:在纯java配置的springbootmvc项目中如何配置Jackson省略延迟加载属性【问题讨论】:【参考方案1】:如果您使用的是SpringBoot,理想情况下您应该已经拥有Hiber 查看详情

使用 spring-boot 以编程方式添加另一个属性资源配置器

】使用spring-boot以编程方式添加另一个属性资源配置器【英文标题】:AddanotherPropertyResourceConfigurerprogramaticallywithspring-boot【发布时间】:2014-12-3109:51:47【问题描述】:我们正在使用gradle、groovy和spring-boot建立一个高级/复杂的多模... 查看详情

Spring Boot:使用 yaml 中配置的 Maven 属性

】SpringBoot:使用yaml中配置的Maven属性【英文标题】:SpringBoot:useofMavenpropertiesconfiguredinyaml【发布时间】:2017-11-0712:31:05【问题描述】:我正在尝试从Maven中提取一些属性并在我的应用程序启动时打印它们。我正在使用SpringBoot,因... 查看详情

Spring Boot 自动配置加载 Spring Kafka 属性失败

...问题描述】:Springboot无法加载属性。这是我通过yaml文件使用的属性。spring:kafka:bootstrap-servers:localhost:9092consumer: 查看详情

Spring Boot如何读取jar外的属性文件

】SpringBoot如何读取jar外的属性文件【英文标题】:Springboothowtoreadpropertiesfileoutsidejar【发布时间】:2017-06-0421:35:49【问题描述】:在我的目标文件夹中,有2个文件夹,lib和conf。所有的properties文件都放在conf文件夹中,jars放在libfo... 查看详情

使用 JPA 进行 Spring Boot 数据源配置 [关闭]

】使用JPA进行SpringBoot数据源配置[关闭]【英文标题】:SpringbootDatasourceconfigwithJPA[closed]【发布时间】:2019-05-0516:00:01【问题描述】:现在我正在从SpringBoot中的应用程序属性文件中获取数据源属性。我想从放置在tomcat7服务器外部... 查看详情

如何使用 Spring Boot 的“spring.jackson.date-format”属性?

】如何使用SpringBoot的“spring.jackson.date-format”属性?【英文标题】:HowtoUseSpringBoot\'s\'spring.jackson.date-format\'Property?【发布时间】:2017-09-2303:38:49【问题描述】:根据CurrentSpringBootReferenceGuide,如果我设置spring.jackson.date-format属性,... 查看详情

有没有办法在 Spring Boot 中获取所有配置属性的名称?

...:2016-08-2008:02:31【问题描述】:我对Spring真的很陌生。我使用springboot提供的外部配置功能。它运行良好,我在几个配置类中使用它,通常只使用默认值。现在我想大致了解我使用的配置属性。这意味着我 查看详情

Cloud Spring Boot 如何使用 yml 文件中的属性

】CloudSpringBoot如何使用yml文件中的属性【英文标题】:Howcloudspringbootusespropertiesinymlfile【发布时间】:2021-05-2303:16:42【问题描述】:当使用SpringCloudAWS进行repo时,我注意到application.yml包含spring:profiles:awscloud:config:enabled:falseaws:params... 查看详情

Spring Boot:自定义属性配置和测试

...dtests【发布时间】:2018-11-1400:49:27【问题描述】:我正在使用带有默认application.yml属性文件的SpringBoot2.0。我想将它拆分为单独的属性文件,因为它变得很大。另外我想编写测试来检查属性的正确性:将出现在生产应用程序上下... 查看详情

Spring Boot 使用 Yaml 而不是属性文件

】SpringBoot使用Yaml而不是属性文件【英文标题】:SpringBootusingYamlinsteadofpropertiesfile【发布时间】:2017-01-2107:08:06【问题描述】:我使用SpringBoot。我想使用YAML而不是属性来编写我的配置。由于我使用spring-boot-starter,SnakeYAML库已经... 查看详情

Spring-Boot:如何设置 JDBC 池属性,例如最大连接数?

】Spring-Boot:如何设置JDBC池属性,例如最大连接数?【英文标题】:Spring-Boot:HowdoIsetJDBCpoolpropertieslikemaximumnumberofconnections?【发布时间】:2014-10-2317:54:41【问题描述】:Spring-Boot是一个非常棒的工具,但是在涉及到更高级的配置时... 查看详情

未读取junit spring boot配置文件属性

...ngread【发布时间】:2018-04-2810:53:13【问题描述】:我正在使用springboot从我的junit设置中的属性文件中读取一个值。我无法读取该值。以下是我的内容:-application-test.propertiesmy.user.name=Amar用于创建bean的配置文件 查看详情

Spring Boot如何在属性文件中隐藏密码

】SpringBoot如何在属性文件中隐藏密码【英文标题】:SpringBoothowtohidepasswordsinpropertiesfile【发布时间】:2016-09-2103:26:33【问题描述】:SpringBoot使用属性文件,至少默认情况下,密码是纯文本的。是否有可能以某种方式隐藏/解密这... 查看详情