无法在Spring Boot自动配置中获取属性绑定

问题描述 投票:0回答:1

我在Spring Boot中有一个非常简单的用例来覆盖Spring Actuator的管理端口,但application.yml的属性似乎没有绑定。

这是属性类,

@ConfigurationProperties(prefix = "test.metrics", ignoreUnknownFields = false)
public class MetricsProperties {
    private boolean enabled = true;
    private final Management management = new Management();

    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public static class Management {
        private String contextPath = "/management";
        private int port = 9079;

        public String getContextPath() {
            return this.contextPath;
        }

        public void setContextPath(String contextPath) {
            this.contextPath = contextPath;
        }

        public int getPort() {
            return this.port;
        }

        public void setPort(int port) {
            this.port = port;
        }
    }
}

我的自动配置课,

@Configuration
@EnableConfigurationProperties(MetricsProperties.class)
@AutoConfigureBefore({ ManagementServerPropertiesAutoConfiguration.class })
@ConditionalOnProperty(prefix = "test.metrics", name = "enabled", matchIfMissing = true)
public class MetricsAutoConfiguration {

    @Bean
    public ManagementServerProperties managementServerProperties(MetricsProperties metricsProperties) {
        log.debug("Initializing management context");
        log.debug("{}", metricsProperties); // Always shows 9079
        ManagementServerProperties managementServerProperties = new ManagementServerProperties();
        managementServerProperties.setContextPath(metricsProperties.getManagement().getContextPath());
        managementServerProperties.setPort(metricsProperties.getManagement().getPort());
        managementServerProperties.getSecurity().setEnabled(false);
        return managementServerProperties;
    }
}

我通过spring.factories加载这个自动配置类,

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
co.test.metrics.metricscollector.MetricsAutoConfiguration

但是,拥有application.yml以下属性不起作用。

test:
  metrics:
    management:
      port: 9999

它总是默认为9079端口。

有谁可以指出我在这里做错了什么?我可以看到MetricsProperties被注入,因为它没有扔NPE。

spring spring-boot
1个回答
0
投票

你不能在@ConfigurationProperties类中有最终字段,这些类也应该遵守bean规范(有getter和setter)。让你的课看起来像这样:

@ConfigurationProperties(prefix = "test.metrics", ignoreUnknownFields = false)
public class MetricsProperties {
    private boolean enabled = true;
    @NestedConfigurationProperty
    private Management management;

    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setManagement(Management management) {
        this.management = management;
    }

    public Management getManagement() {
        return this.management;
    }

    public static class Management {
        private String contextPath = "/management";
        private int port = 9079;

        public String getContextPath() {
            return this.contextPath;
        }

        public void setContextPath(String contextPath) {
            this.contextPath = contextPath;
        }

        public int getPort() {
            return this.port;
        }

        public void setPort(int port) {
            this.port = port;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.