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

时间:2018-03-05 02:34:10

标签: spring spring-boot

我在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。

1 个答案:

答案 0 :(得分: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;
        }
    }
}