Skip to main content

@bean VS @component



# @Component @Bean
1 @Component is a class-level annotation @Bean is a method-level annotation
2 class-level annotation that is used to mark a class as a Spring component, indicating that it should be automatically detected and registered as a bean in the Spring application context. The @Component annotation is a generic stereotype annotation that can be used to annotate any class. method-level annotation that is used to explicitly declare a bean definition in a configuration class. It is typically used when a class does not have the @Component annotation, or when you want more fine-grained control over the configuration of the bean.
3 While @Component is used for automatic scanning and registering of beans While @Bean is used for explicit manual registration of beans.
4 @Component is used for general classes while @Bean is used for creating a specific instance of a class and providing it to the Spring container.
5 In short, @Component is used to annotate a class and automatically detect and register it as a bean whereas @Bean is used to declare a method that provides an instance of a bean to the Spring container.

Add new comment

Restricted HTML

  • You can use shortcode for block builder module. You can visit admin/structure/gavias_blockbuilder and get shortcode, sample [gbb name="page_home_1"].
  • You can use shortcode for block builder module. You can visit admin/structure/gavias_blockbuilder and get shortcode, sample [gbb name="page_home_1"].