Compare @Component (v2.5) versus @Bean (v 3.0)

Technology CommunityCategory: SpringCompare @Component (v2.5) versus @Bean (v 3.0)
VietMX Staff asked 3 years ago
Problem

Would it have been possible to re-use the @Component annotation instead of introducing @Bean annotation?

@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

To answer your question: Sure, probably; but they chose not to, since the two are quite different. Spring’s already confusing enough without muddying the waters further.