12

Am I correct, thinking that @Bean and @Component annotations are the same, but first is for classes and another is for methods?

In both cases container just creates appropriate beans, right?

Luiggi Mendoza
  • 83,472
  • 15
  • 149
  • 315
Suzan Cioc
  • 27,971
  • 54
  • 206
  • 370

1 Answers1

12

Not quite. They belong to different concepts.

@Component is a stereotype annotation. A class annotated with this annotation will be auto detected during class path scanning. See also Spring reference documentation chapter 5.10. Other than that the @Bean annotation belongs to the Java configuration feature. Within a configuration class this annotation is used to mark methods that define beans.

Abdullah Khan
  • 10,786
  • 6
  • 60
  • 71
Christian Frommeyer
  • 1,280
  • 1
  • 11
  • 20
  • 1
    A better answer than in the linked duplicate question. This answer here would still a bit better with easy examples :) – PowerFlower May 01 '17 at 15:16