【问题标题】:Cannot check for missing property if declared in @ConfigurationProperty如果在 @ConfigurationProperty 中声明,则无法检查缺少的属性
【发布时间】:2017-04-24 22:43:55
【问题描述】:

我们已经构建了一个 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();
}

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

我们使用的是 Spring Boot 1.4

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

【问题讨论】:

  • 你写@ConditionProperties时是说@ConfigurationProperties
  • 我写了一个单元测试,我认为它可以练习你的场景,但我无法重现你的失败。我正在使用 1.4.1 版本的 SpringBoot,尝试升级作为潜在的解决方案。如果需要,我会发布单元测试。
  • @BobLukens 是的,欢迎进行单元测试。 :) 是的,在第二个代码 sn-p 下面的段落中,我的意思是 ConfigurationProperties 而不是 ConditionProperties。 ;)

标签: spring spring-boot


【解决方案1】:

这是我整理的一个单元测试,我相信它可以捕捉到您的场景,但我无法重现失败。您可以查看并确定是否不同或不正确。我正在使用 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);
    }
}

【讨论】:

  • 感谢您提供有用的测试用例!可悲的是,在我的机器(MacOS Sierra,JDK 1.8.102)上,第一个断言已经失败。我在 Github 上分享了整个项目的测试:github.com/rjayasinghe/spring-conditional-test
  • 愚蠢的我忘了在测试用例中创建一个映射到您的 cmets 的属性文件。 ;) 现在它按预期工作。 :)
  • 将此添加到您的 application.properties 文件中:foo.bar.baz=moo。测试经历了 4 个场景 - 1. 当属性定义为匹配值时有条件地创建 bean,2. 当属性定义但值不匹配时有条件地创建 bean 3. 当属性未定义时有条件地创建 bean 4. 有条件地未定义属性时不要创建 bean
【解决方案2】:

spring项目中已经报告了一个问题,你结帐讨论https://github.com/spring-projects/spring-boot/issues/2282

【讨论】:

  • 太棒了。该错误报告验证了我的假设。 :)
  • 尽管他们报告的行为与我观察到的相反 - 这让我有了很好的洞察力。 ;)
猜你喜欢
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多