如果在@ConfigurationProperty中声明,则无法检查缺少的属性

时间:2016-12-09 12:37:07

标签: spring spring-boot

我们已经构建了一个Spring Boot启动器,它在使用@ConfigurationProperties注释的类中描述了它的配置:

@ConfigurationProperties(name = "foo.bar")
public class FooConfiguration {
    public String baz;
}

在首发之外,我想根据是否使用@ConditionalOnProperty注释设置 属性来执行某些操作:

@Bean
@ConditionalOnProperty(name = "foo.bar.baz", matchIfMissing=true)
public FooReplacement moo() {
    return new FooReplacement();
}

但是,如果在使用@ConditionProperties注释的类中描述了foo.bar.baz属性,则此条件永远不会匹配。如果没有在这样的类中描述,@ ConstditionalOnProperty注释就可以正常工作。

我们正在使用Spring Boot 1.4

那么,Spring Boot的工作方式是预期的还是我们发现了一个错误?

2 个答案:

答案 0 :(得分:3)

这是我放在一起的单元测试,我相信捕获你的场景,但我无法重现失败。您可以查看并确定是否不同或不正确。我使用的是Spring Boot 1.4.1。希望这可以帮助您解决问题。

package com.sbp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = PropsTest.TestContext.class)
public class PropsTest {

    @Configuration
    @EnableConfigurationProperties // needed since im not loading a class annotated with @SpringBootApplication
    public static class TestContext {

        // this exists in properties file (im using application.yaml) with value = moo
        // foo.bar.baz: moo
        @Component
        @ConfigurationProperties(exceptionIfInvalid = true, value = "foo.bar")
        public static class FooConfiguration {
            private String baz;

            public String getBaz() {
                return baz;
            }

            public void setBaz(String baz) {
                this.baz = baz;
            }
        }

        // this property doesn't exist in properties file
        @Component
        @ConfigurationProperties(exceptionIfInvalid = true, value = "foo.bar1")
        public static class Foo1Configuration {
            private String baz;

            public String getBaz() {
                return baz;
            }

            public void setBaz(String baz) {
                this.baz = baz;
            }
        }

        @Bean
        @ConditionalOnProperty(name = "foo.bar.baz", matchIfMissing = false, havingValue = "moo")
        public FooReplacement moo() {
            return new FooReplacement();
        }

        @Bean
        @ConditionalOnProperty(name = "foo.bar.baz", matchIfMissing = false, havingValue = "noo")
        public FooReplacement noo() {
            return new FooReplacement();
        }

        // i think this is your scenario
        @Bean
        @ConditionalOnProperty(name = "foo.bar1.baz", matchIfMissing = true)
        public FooReplacement ooo() {
            return new FooReplacement();
        }

        @Bean
        @ConditionalOnProperty(name = "foo.bar1.baz", matchIfMissing = false)
        public FooReplacement qoo() {
            return new FooReplacement();
        }
    }

    public static class FooReplacement {
    }

    @Autowired
    private ApplicationContext ctx;

    @Autowired
    private TestContext.FooConfiguration fooConfiguration;

    @Autowired
    private TestContext.Foo1Configuration foo1Configuration;

    @Autowired
    private Environment environment;

    @Test
    public void propertyNotDefined_shouldCreateBean() {
        assertTrue(ctx.containsBean("moo"));
        assertFalse(ctx.containsBean("noo"));
        assertTrue(ctx.containsBean("ooo"));
        assertFalse(ctx.containsBean("qoo"));

        assertNull(environment.getProperty("foo.bar1.baz", String.class));
        assertEquals("moo", environment.getProperty("foo.bar.baz", String.class));

        assertNull(foo1Configuration.baz);
        assertEquals("moo", fooConfiguration.baz);
    }
}

答案 1 :(得分:0)

春季项目已经报道了一个问题,您可以查看讨论https://github.com/spring-projects/spring-boot/issues/2282