Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如果在@ConfigurationProperty中声明,则无法检查缺少的属性_Spring_Spring Boot - Fatal编程技术网

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

Spring 如果在@ConfigurationProperty中声明,则无法检查缺少的属性,spring,spring-boot,Spring,Spring Boot,我们构建了一个Spring启动程序,它在一个用@ConfigurationProperties注释的类中描述它的配置: @ConfigurationProperties(name = "foo.bar") public class FooConfiguration { public String baz; } 在starter之外,我想根据是否使用@ConditionalOnProperty注释设置属性来执行一些操作: @Bean @ConditionalOnProperty(name

我们构建了一个Spring启动程序,它在一个用@ConfigurationProperties注释的类中描述它的配置:

@ConfigurationProperties(name = "foo.bar")
public class FooConfiguration {
    public String baz;
}
在starter之外,我想根据是否使用@ConditionalOnProperty注释设置属性来执行一些操作:

@Bean
@ConditionalOnProperty(name = "foo.bar.baz", matchIfMissing=true)
public FooReplacement moo() {
    return new FooReplacement();
}
但是,如果在用@ConditionProperties注释的类中描述foo.bar.baz属性,则此条件永远不匹配。如果没有在这样的类中进行描述,@ConditionalOnProperty注释就可以正常工作

我们使用的是SpringBoot1.4


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

Spring project中已经报告了一个问题,您可以查看讨论

这里是我编写的一个单元测试,我相信它捕获了您的场景,但我无法重新创建失败。您可以查看并确定是否不同或不正确。我使用的是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);
    }
}

你的意思是
@ConfigurationProperties
当你编写
@ConditionProperties
时,我编写了一个单元测试,我认为它可以模拟你的场景,但我无法重现你的失败。我正在使用SpringBoot的1.4.1版本,尝试升级作为一个潜在的解决方案。如果您需要,我会发布单元测试。@BobLukens是的,欢迎进行单元测试。:)是的,在下面的第二段代码中,我指的是ConfigurationProperties,而不是ConditionProperties伟大的该错误报告验证了我的假设。:)虽然他们报告了与我观察到的相反的行为,但这给了我一个很好的洞察力谢谢你有用的测试用例!不幸的是,在我的机器(MacOS Sierra,JDK 1.8.102)上,第一个断言已经失败了。我在Github上共享了整个测试项目:愚蠢的我忘了在testcase中创建一个属性文件映射到您的注释现在它按预期工作。:)将其添加到application.properties文件:
foo.bar.baz=moo
。该测试经历了4个场景-1。当使用匹配值2定义属性时,有条件地创建bean。当定义了属性但值与3不匹配时,有条件地创建bean。在未定义属性4时有条件地创建bean。当未定义属性时,有条件地不创建bean