Classes annotated with any of the stereotype component annotations (@Component, @Service, @Repository etc), can also expose new bean definitions using @Bean annotation on their methods.
@Component classes are not CGLIB proxied
Classes annotated with @Configuration are CGLIB proxied. @Component classes, however, are not enhanced with CGILIB to intercept the bean method invocation, that means a direct call will not route through the container and will return a new instance every time.
In the component classes, we can use any valid annotation along with @Bean method and it's parameters, exactly the same way we use them in @Configuration classes (e.g. @Lazy, @Qualifier, @Primary etc).
Lite @Beans mode
When @Bean methods' enclosing class is not annotated with @Configuration like in above @Component case, they are said to operating in a 'lite' mode.
According to Spring reference doc :
Full @Configuration vs 'lite' @Beans mode?
When @Bean methods are declared within classes that are not annotated with @Configuration they are referred to as being processed in a 'lite' mode. For example, bean methods declared in a @Component or even in a plain old class will be considered 'lite'.
Unlike full @Configuration, lite @Bean methods cannot easily declare inter-bean dependencies. Usually one @Bean method should not invoke another @Bean method when operating in 'lite' mode.
Only using @Bean methods within @Configuration classes is a recommended approach of ensuring that 'full' mode is always used. This will prevent the same @Bean method from accidentally being invoked multiple times and helps to reduce subtle bugs that can be hard to track down when operating in 'lite' mode.
@Configuration classes can also be scanned
We can even use @Configuration classes instead of @Component classes to achieve the same. @Configuration basically itself is a component (it's annotated with @Component). In that case, the methods annotated with @Bean will be CGILIB enhanced and will be proxied.
AnnotationConfigApplicationContext and @Component classes
AnnotationConfigApplicationContext is an implementation of ApplicationContext to bootstrap the Spring container. Other than @Configuration classes, AnnotationConfigApplicationContext is capable to accept the class(s) annotated with @Component (or any other stereotype) as well, but as mentioned above doing so will cause the beans to operate in lite mode.
Possible use cases
The primary purpose of defining beans that way is: we want our component beans to act like a factory. Other than that, there might be some scenarios where we cannot modify the main @Configuration(s) files or we want to define/customize beans only within a particular module.
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|