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
@Bean
'lite' mode not supported if @Bean
methods are not declared locally
#30449
Comments
@quaff neither? This has nothing to do with |
@snicoll I know option 2 is recommended, I think there is something arguable here. |
That's not a recommendation. The stereotype must be applied, otherwise the context is using a fallback called "lite mode".
Yes and it's then treated with "lite mode", see https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-java-basic-concepts |
AFAIK, the |
I don't believe this is specific to Rather, I believe the described behavior is due to the semantics of But that's just an assumption. I'd have to create tests to verify/debug it. |
This method doesn't check super class has bean methods, I'm trying to provide PR. Lines 179 to 189 in 993a69d
|
Sam, your quote is outdated. I already explained I thought this was a "feature" of lite mode. Whatever the underlying util this calls, it's very odd that it behaves differently than what |
Yes, It split to two issues.
|
@snicoll, my quote is not outdated. I was replying directly to your previous question:
As I pointed out in #30449 (comment), it's not Rather, it's standard behavior from |
Agreed, if the semantics between class registration and |
@Bean
_lite_ mode not supported if @Bean
methods are not declared locally
@Bean
_lite_ mode not supported if @Bean
methods are not declared locally@Bean
'lite' mode not supported if @Bean
methods are not declared locally
Again, this is not related to Rather, this is the standard behavior for a For that use case, a non-annotated class must have a local In contrast, any class imported with Lines 509 to 515 in 993a69d
Technically speaking, changing that behavior would be a breaking change. So if we decide to do that, it would likely have to be in 6.1 @jhoeller, thoughts? |
It explain the difference and it's expected behavior, we should improve the document.
IMO it's a fix and should be backported to 5.x also. |
…te mode ConfigurationClassUtils should check bean methods in super classes also Fix spring-projectsGH-30449
It wasn't a quote so it can't be. Sorry, I thought you were quoting me. The PR that supersedes this issue has been closed and I agree with that but I still wonder if any other user-facing API ends up using the code in What I meant with outdated is that I now think this is a problem with |
Ahhh, OK. I understand the confusion now. Thanks for clarifying. 👍
I only partially agree with that, which I will elaborate on below.
Yes, this is the "standard" behavior for "annotated classes" that are registered directly with an For example, the following demonstrates the same undesired behavior without using any part of the TestContext framework. class BeanLiteModeTests {
@Test
void test() {
try (var context = new AnnotationConfigApplicationContext(Config.class)) {
var bean1 = context.getBean("bean1", String.class);
var bean2 = context.getBean("bean2", String.class);
assertThat(bean1).isEqualTo("bean1");
assertThat(bean2).isEqualTo("bean2");
}
}
// @org.springframework.stereotype.Component
static class Config extends BaseConfig {
}
static class BaseConfig {
@Bean
String bean1() {
return "bean1";
}
@Bean
String bean2() {
return "bean2";
}
}
}
Yes, I agree:
Based on the example I provided above, I would say it's an issue with "registering classes directly with an application context" in general.
Right. If the classes are picked up via component scanning, I agree that we should not scan the class hierarchy for
That's the key differentiator! Any time the user supplies In other words, whenever a If we made a change along those lines, then
Yes, thanks for reopening the issue. |
@jhoeller, what do you think about having consistent semantics for I suppose the alternative would be to change the behavior of |
Update: I've prototyped an implementation for this which treats any main...sbrannen:spring-framework:issues/gh-30449-bean-lite-mode-for-registered-classes |
This has been addressed in commit a455317 and can be tested in upcoming snapshots for 6.0.10. |
Prior to this commit, if a non-annotated [1] class was registered directly with an ApplicationContext -- for example, via AnnotatedBeanDefinitionReader, AnnotationConfigApplicationContext, or @ContextConfiguration -- it was not considered a configuration class in 'lite' mode unless @bean methods were declared locally. In other words, if the registered class didn't declare local @bean methods but rather extended a class that declared @bean methods, then the registered class was not parsed as a @configuration class in 'lite' mode, and the @bean methods were ignored. Whereas, a non-annotated class registered via @import is always considered to be a configuration class candidate. To address this discrepancy between @import'ed classes and classes registered directly with an ApplicationContext, this commit treats any class registered via AnnotatedBeanDefinitionReader as a @configuration class candidate with @bean 'lite' mode semantics. [1] In this context, "non-annotated" means a class not annotated (or meta-annotated) with @component, @componentscan, @import, or @ImportResource. Closes spring-projectsgh-30449
I'm not sure it's a bug or undocumented limitation.
If no
@Bean
defined inConfig
then@Bean
defined inBaseConfig
will be ignored.there are many workarounds
@Bean
toConfig
@Configuration
or@Component
onConfig
@Import
instead ofContextConfiguration
The text was updated successfully, but these errors were encountered: