Spring-boot: @ConfigurationPropertiesScan not compatible with @Profile @ConfigurationProperties

Created on 20 Apr 2019  路  3Comments  路  Source: spring-projects/spring-boot

With the new way of scanning @ConfigurationProperties with @ConfigurationPropertiesScan, a @ConfigurationProperties annotated class is not instanciated when @Profile is used in conjunction:

@Profile("mail")
@ConfigurationProperties("mail")
public class MailProperties {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

The full demo repo is available here.

bug

Most helpful comment

This is a bug and my guess is that the ClassPathScanningCandidateComponentProvider used for scanning @ConfigurationProperties doesn't have the Environment set and hence doesn't have access to the active profiles in the environment.

All 3 comments

Spring Boot 2.0.0.M2 documentation states that -

Any @Component or @Configuration can be marked with @Profile to limit when it is loaded

So I don't think annotating @ConfigurationProperties classes with @Profile is a correct use. Although following implementation should work -

@Configuration
@Profile("mail")
public class MailPropertiesConfiguration
{
    @ConfigurationProperties("mail")
    public static class MailProperties
    {
        private String address;

        public String getAddress()
        {
            return address;
        }

        public void setAddress(String address)
        {
            this.address = address;
        }
    }
}

Reference : https://docs.spring.io/spring-boot/docs/2.2.0.M2/reference/html/spring-boot-features.html#boot-features-profiles

Yes, that's exactly my point, that @ConfigurationProperties/@ConfigurationPropertiesScan should work like for instance @Repository/@EnableJpaRepositories which supports @Profile.

Using a more complex code like you did doesn't make sense to me as this new feature is supposed to simplify @ConfigurationProperties classes (by removing @Component).

This is a bug and my guess is that the ClassPathScanningCandidateComponentProvider used for scanning @ConfigurationProperties doesn't have the Environment set and hence doesn't have access to the active profiles in the environment.

Was this page helpful?
0 / 5 - 0 ratings