I understand that @Component
annotation was introduced in spring 2.5 in order to get rid of xml bean definition by using classpath scanning.
@Bean
was introduced in spring 3.0 and can be used with @Configuration
in order to fully get rid of xml file and use java config instead.
Would it have been possible to re-use the @Component
annotation instead of introducing @Bean
annotation? My understanding is that the final goal is to create beans in both cases.
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.
It is used to explicitly declare a single bean, rather than letting Spring do it automatically. If any class is annotated with @Component it will be automatically detect by using classpath scan. We should use @bean, if you want specific implementation based on dynamic condition.
Technically, there is no difference between them, but every auto component scan annotation should be used for a special purpose and within the defined layer. @Component : It is a basic auto component scan annotation, it indicates annotated class is an auto scan component.
Spring Component annotation is used to denote a class as Component. It means that Spring framework will autodetect these classes for dependency injection when annotation-based configuration and classpath scanning is used.
@Component Preferable for component scanning and automatic wiring.
When should you use @Bean?
Sometimes automatic configuration is not an option. When? Let's imagine that you want to wire components from 3rd-party libraries (you don't have the source code so you can't annotate its classes with @Component), so automatic configuration is not possible.
The @Bean annotation returns an object that spring should register as bean in application context. The body of the method bears the logic responsible for creating the instance.
@Component
and @Bean
do two quite different things, and shouldn't be confused.
@Component
(and @Service
and @Repository
) are used to auto-detect and auto-configure beans using classpath scanning. There's an implicit one-to-one mapping between the annotated class and the bean (i.e. one bean per class). Control of wiring is quite limited with this approach, since it's purely declarative.
@Bean
is used to explicitly declare a single bean, rather than letting Spring do it automatically as above. It decouples the declaration of the bean from the class definition, and lets you create and configure beans exactly how you choose.
To answer your question...
would it have been possible to re-use the
@Component
annotation instead of introducing@Bean
annotation?
Sure, probably; but they chose not to, since the two are quite different. Spring's already confusing enough without muddying the waters further.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With