-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Enabling configuration properties scanning by default prevents conditional registration of @ConfigurationProperties-annoted types that are found by scanning #18674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I understand that If you want to conditionally create such bean, you shouldn't put them in a package that is target for classpath scanning (auto-configurations shouldn't use package scanning in the first place). If you This is based on partial code snippet so I might be missing something. Does that make sense? |
@snicoll Thanks for the quick explanations. I see what you meant and I just noticed this on the upgrade instructions:
We wanted to put the our service bean together with its configuration, and use |
And that's a perfectly reasonable thing to do. I can see now how scanning user-config makes it harder. Flagging for team attention to see how we could mitigate this. |
One idea we discussed is adding a |
I got a similar problem. We have a application that can talk with oidc provider or ldap e.g. and for each security provider we have a guarded configuration class (see here e.g. https://github.com/synyx/urlaubsverwaltung/blob/master/src/main/java/org/synyx/urlaubsverwaltung/security/oidc/OidcSecurityConfiguration.java) and this will only be executed if But now |
I'm starting to feel that configuration properties scanning may have been a mistake due to the dual purposes that |
My hunch when I was disabling this as we upgraded was that |
I feel the same. I have at the moment no clue how I could achieve my requirement with Spring Boot 2.2. It feels like we "destroyed" the mechanism to have a "namespaced" configuration class with |
Wouldn't a solution be to annotate the the configuration properties class with @Component
@ConditionalOnBean(FooBarConfig.class)
@EnableConfigurationProperties(FooBarConfig.class)
public class FooBar {
public FooBar() {
Logger.getGlobal().info("Starting FooBar...");
}
}
@ConditionalOnProperty(prefix = "foo", name = "enabled")
@ConfigurationProperties(prefix = "foo")
@Validated
public class FooBarConfig {
@NotNull
String bar;
public void setBar(String bar) {
this.bar = bar;
}
} |
Thanks for the suggestion, @evpaassen. We discourage use of |
We discussed this last week and decided that we're no longer going to enable configuration property scanning by default. Strictly speaking, this will be a breaking change from 2.2.0 but so was the loss of the ability to enable configuration properties conditionally. Making scanning opt-in will allow those that relied on 2.1.x's behaviour to continue to have it while also allowing those who want configuration property scanning to enable it by adding |
I've broken some of the smoke tests. |
So me 2.2.0 -> 2.2.1 migration is not going smoothly I was using
Now i am getting
I am a bit confused about what changed and how to undo the changes from 2.2.1. Better description in the release notes regarding breaking changes would be most welcome. |
@fkowal thanks for letting us know but that looks like a regression unrelated to this issue. Please create a separate issue. |
My example in @Configuration
@ConfigurationPropertiesScan
class YamlParser(private val appInfo: ApplicationInfo, private val servletInfo: ServletInfo) {
companion object{
fun printDomain() = println(" \uD83C\uDFAF Target domain: https://$domain")
}
@PostConstruct
fun readApplicationProperties() {
domain = appInfo.domain
contentFolder = appInfo.contentFolder
contextPath = servletInfo.contextPath
}
}
@ConfigurationProperties(prefix = "application") @ConstructorBinding
data class ApplicationInfo(val domain: String, val contentFolder: String)
@ConfigurationProperties(prefix = "server.servlet") @ConstructorBinding
data class ServletInfo(val contextPath: String) |
Are the 2.2 release notes correct? The statement "[we] decided that we're no longer going to enable configuration property scanning by default" in this issue seems to contradict the statement "If you use @SpringBootApplication, scanning is enabled by default for the package that contains the @SpringBootApplication-annotated class." in the release notes. While pedantically this issue supersedes the 2.2 release notes if they're treated as 2.2.0 release notes, pragmatically the release notes will get far more eyeballs on them than this ticket. |
Thanks, @emersonf. That was an oversight on our part. I've updated the release notes to hopefully clarify things. |
Uh oh!
There was an error while loading. Please reload this page.
Hello,
We were trying to migrate from 2.1.9 to 2.2.0 for our applications last week and encountered a startup failure due to a configuration validation failure. In short, if we are using
@EnableConfigurationProperties
combined with@ConditionalOnProperty
for some beans, the new version is not respecting the conditional on the config part.Here is a tiny example:
If we add this component to any Spring Boot application, it will fail to start with 2.2.0.
Expected behavior:
The bean initialization of
FooBar
andFooBarConfig
should be skipped because@ConditionalOnProperty
doesn't match.Actual behavior:
FooBar
is skipped but theFooBarConfig
keeps initializing thus failed startup.The text was updated successfully, but these errors were encountered: