Issue with Spring Boot 2.3.3.RELEASE
I have provided an example project of what I'm trying to accomplish here.
The problem relates to @ConditionalOnProperty annotation when used at the config class level to enumerate based on a properties file (purposely not application-*.properties) that has been loaded via a @PropertySource (could be the same or different configuration class, doesn't matter).
Based on the documentation of @Conditional, when used at the config class level, all the beans used in that class should be subjected to that condition. However, this is not working as intended because the bean in my example will only load when @ConditionalOnProperty is used at the bean level.
Is this a gotcha to be aware of when using these in tandem? I would assume that @ConditionalOnProperty would work at the config class level and apply that same conditional to all the beans declared in the config.
Thanks!
Comment From: snicoll
@napapu unfortunately, we do not recommend to use @PropertySource
as it is not processed at the same time as the rest of the Environment
. By the time the condition applies, the @PropertySource
may not have been processed which is why you see a difference between applying the condition on the bean or the class.
The documentation describes how you can tune the Environment. Note that as of Spring Boot 2.4 there are various improvements in that area as well, check this blog post.