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
  • 85,076
  • 16
  • 154
  • 332
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

1 Answers1

14

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
  • 12,010
  • 6
  • 65
  • 78
Christian Frommeyer
  • 1,390
  • 1
  • 12
  • 20
  • 2
    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