当前位置: 首页 > 知识库问答 >
问题:

如何将属性与Spring Boot配置属性分组

漆雕升
2023-03-14
@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不会加载嵌套类中的属性

共有1个答案

孟绪
2023-03-14

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

属性文件

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;
    }
}
 类似资料:
  • 简介 Apache ShardingSphere 提供属性配置的方式配置系统级配置。 配置项说明 名称 数据类型 说明 默认值 sql-show (?) boolean 是否在日志中打印 SQL。 打印 SQL 可以帮助开发者快速定位系统问题。日志内容包含:逻辑 SQL,真实 SQL 和 SQL 解析结果。 如果开启配置,日志将使用 Topic ShardingSphere-SQL,日志级别是 I

  • 简介 Apache ShardingSphere 提供属性配置的方式配置系统级配置。 配置项说明 名称 数据类型 说明 默认值 sql-show (?) boolean 是否在日志中打印 SQL。 打印 SQL 可以帮助开发者快速定位系统问题。日志内容包含:逻辑 SQL,真实 SQL 和 SQL 解析结果。 如果开启配置,日志将使用 Topic ShardingSphere-SQL,日志级别是 I

  • 我需要从正在运行的实例中转储springboot应用程序属性,可以吗?我需要它的原因:我正在使用链接配置文件,但其中一个属性设置不正确。谢谢

  • 我希望将concur严格用作配置源。我正在使用spring cloud Consor配置来获取配置。我正在使用git2consul将文件加载到Consor并读取它们。根据spring云文档,我在构建中添加了以下内容。格拉德尔 并在我的application.properties 我面临的问题是,预期的属性没有加载到ConfigurationProperties bean中。在ConsultProp

  • 问题内容: 是HTML的属性吗? 我应该做还是 我读了很多文章,但仍然感到困惑。 有人可以向我解释一下HTML / JS中的属性与属性之间的区别是非常简单的吗? 问题答案: 属性由HTML定义。属性(在DOM元素上)由DOM(以及HTML 5定义,模糊了标记和DOM之间的边界)。 一些HTML属性具有1:1映射到属性。就是这样的一个例子。 有时名称是不同的。该属性映射到该属性,该属性映射到该属性(

  • 主要内容:1. 概述,2. 源码解析1. 概述 属性配置 如果你的应用足够简单,例如,不需要多注册中心或多协议,并且需要在spring容器中共享配置,那么,我们可以直接使用 dubbo.properties作为默认配置。 Dubbo可以自动加载classpath根目录下的dubbo.properties,但是你同样可以使用JVM参数来指定路径:-Ddubbo.properties.file=xxx.properties。 重写与优先