• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

@Bean and @Component

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true if I use @Component (or @Named), I should include this class' package in the component scan's base package, but if I use @Bean then I don't have to make this class be inside any component scan package ?
 
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need to declare the packages that need to be scanned for components, but the @Bean annotation is used on methods and not on classes, so component scanning doesn't apply to this annotation.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bean is basically used on methods in @Configuration class.
@Component is used on class that signifies it is a Bean.

If you wrote all classes under the main package then you don't need @ComponentScan in Spring Boot. It will scan automatically.
But if you wrote the classes in different package than the main() method's class. Then you have to let Spring container know which packages to scan. You write them within @ComponentScan("xyz")
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!