-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
In the Basic Concepts: @Bean and @Configuration section exists the following Full @Configuration vs “lite” @Bean mode?
block with the following first paragraph:
When @Bean methods are declared within classes that are not annotated with @Configuration,
they are referred to as being processed in a “lite” mode. Bean methods declared in a @Component or
even in a plain old class are considered to be “lite”, with a different primary purpose of the
containing class and a @Bean method being a sort of bonus there. For example, service components may
expose management views to the container through an additional @Bean method on each applicable
component class. In such scenarios, @Bean methods are a general-purpose factory method mechanism.
... more
That big block does not contain none information about:
- Why the
lite
bean approach exists? - When the
lite
bean approach is mandatory over thefull
approach? (Here should appear the differences between them)
Now about sample/snippet code:
The full
approach is:
@Configuration
class XConfig {
@Bean
X defineX(){
...
return x;
}
}
It is the classic and most known approach. About the lite
approach, first its introduction:
Introduction
When @Bean methods are declared within classes that are not annotated with @Configuration
And according with the big block shown above exists two ways
One
Bean methods declared in a @Component
Therefore
@Component/@Service
class XSomething {
...
@Bean
X defineX(){
...
return x;
}
}
Until here the XSomething
class can be detected and managed by Spring thanks to the combination of @ComponentScan
together with @Component/@Service
, therefore @Bean
can be used by Spring too.
Two
or even in a plain old class
Here I am assuming the following:
//No annotation
class XSomething {
...
@Bean
X defineX(){
...
return x;
}
}
Therefore the current documentation does not cover "two" through some snippet code the answer of the following question:
- How does Spring apply here the "lite" bean approach through a "plain old class"?
If the @Bean
is totally isolated or is not declared with neither @Configuration
nor @Component
. So How @Bean
can be used by Spring?
Please clarify two because is not clear in the current documentation - there is neither a snippet nor sample code about this approach, perhaps I misinterpreted the or even in a plain old class
part.
I already wrote a question on SO about the question 3 at:
But I think is valuable for the community put in that block - or create a dedicated section - explaining these 3 questions together.
Thanks in advance for your understanding.