-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Doc: Consistent @Profile declarations on overloaded @Bean methods [SPR-15266] #19831
Comments
Juergen Hoeller commented In general, metadata on Also, So the general recommendation is to declare the same |
Juergen Hoeller commented From a compatibility perspective, we need to leniently allow for conditions to be expressed on the first encounter of a method of a particular name, consistently applying to all factory methods of the same bean (with the same method name). As a consequence, the arrangement above - unintuitively - only registers bean "valueForTest" if the profile on the first processed method (as of 4.3.6 reliably in declaration order) is active, but that's at least consistent in the detection algorithm where we start processing at the concrete class level and make our way up through the class hierarchy, with the first encounter of a particular method overriding later occurrences. Since it is tricky to enforce consistent declarations in a way that doesn't break existing valid arrangements, I'm rather turning this into a documentation task. As general advice, I'd recommend against overloaded |
Sam Brannen commented Juergen Hoeller, just to clarify, are you saying the following common configuration pattern is not actually supported? @Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@Profile("prod")
public DataSource productionDataSource() {
return // production data source
}
@Bean(name = "dataSource")
@Profile("dev")
public String developmentDataSource() {
return // development data source
}
} |
Juergen Hoeller commented Distinct The problem only comes in when the method names are the same: We consider this the very same definition then, just with overloaded "constructors". Any attempt to create that bean is going to resolve all factory methods of the same name in such a scenario, selecting among them depending on the availability of their required arguments. Individual overloaded methods can't be excluded through conditions, just distinctly named methods can (independent from their argument signature). Aside from the documentation, I'm still trying to improve the error reporting in such a scenario. This turns out to be non-trivial though. |
Sam Brannen commented Thanks for the detailed clarification! And yes, I'd recommend that the documentation be very explicit with regard to the two scenarios mentioned above -- in order not to confuse developers about the applicability of the supported scenario that I provided. ;-) |
Juergen Hoeller commented Reopened for one more pass over the docs, differentiating between the |
Sam Brannen commented The revisions in your latest commit look much better. Thanks, Juergen! (y) |
Georg Herdt opened SPR-15266 and commented
When having bean factory methods with the same name but different arguments used for creating instance for different profiles the bean is not recognized for some profiles.
See example below:
Corresponding test will fail:
When using profile "B" dependencies for bean named "something" will be created successfully. Providing string value "bbb".
For profile "A" it is not working properly.
Behaviour depends on used Spring version. For releases 4.0.0. up to 4.1.4 outcome will be "bbb".
I would expect "aaa".
From release 4.1.5 on (including 5.x.x releases) a NoSuchBeanDefinitionException is thrown.
See the attached gradle project to evaluate the behaviour.
Affects: 4.3.6, 5.0 M4
Attachments:
Issue Links:
@Bean
declarations with same primary bean name do not work anymore@Bean
registration order within Class-reflected configuration classesReferenced from: commits 5d3249f, 6c370ed, 022aefd
The text was updated successfully, but these errors were encountered: