Enhance conditional bean creation when using method overloading #28019
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request closes the issue mentioned here: #22609
Short summary:
A bean won't be created when it is being skipped by a condition for example when using
@ConditionalOnProperty
or@ConditionalOnExpression
because the condition is evaluated to false. However a second bean with exactly the same name with different parameters may evaluate to true because the condition has met, so this bean should be created but that does not happen.An example code snippet:
When
some-property-enabled
is false and some-different-property is true the second method should be called and the bean should be created, however this does not happen. The bean name is already in the to be skipped beans list.The pull request contains the changes to reconsider a skipped bean when it encounters a new bean definition having a different condition. If it should not be skipped it will be created. The PR also takes into inheritance into consideration (for backwards compatibility, see
spring-framework/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassWithConditionTests.java
Line 139 in c263cbf
In that specific use case the parent class defines a bean which should be created, however it is getting overridden by a subclass which defines a condition on the bean, so it will be created conditionally and may not even be created when it evaluates to false.
This PR uses a wrapper for the MethodMetadata as a helper class for resolving the inheritance with the condition on a subclass when it still needs to be skipped.
My use case
In my use case I wanted to refactor my code base. I have the bean configuration below:
github/mutual-tls-ssl/SSLConfig.java
And I wanted to refactor it to the following snippet:
However it fails. But with the code changes of the Pull request it will create one of the beans when the conditions is true.