roborob
roborob

Reputation: 63

Cannot check for missing property if declared in @ConfigurationProperty

We have built a Spring Boot starter that describes it's configuration in a class annotated with @ConfigurationProperties:

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

Outside the starter I want to perform some action depending on whether the property is not set using the @ConditionalOnProperty annotation:

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

However, this condition never matches if the foo.bar.baz property is described in a class annotated with @ConditionProperties. If it's not described in such a class, the @ConditionalOnProperty annotation works just fine.

We are using Spring Boot 1.4

So, is the way Spring Boot is expected to work or have we spotted a bug?

Upvotes: 4

Views: 14383

Answers (2)

Bob Lukens
Bob Lukens

Reputation: 720

Here is a unit test I put together that I believe capture your scenario but I was unable to recreate the failure. You can review and determine if different or incorrect. I'm using Spring Boot 1.4.1. Hope this helps you get past the issue.

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);
    }
}

Upvotes: 4

Nagaraddi
Nagaraddi

Reputation: 279

There is an issue already reported in spring project, you checkout discussion https://github.com/spring-projects/spring-boot/issues/2282

Upvotes: 0

Related Questions