As discussed in one of our previous blog posts on Spring stereotype annotations, @Component is a stereotype annotation in the Spring Framework. It is used to mark a Java class as a Spring-managed bean. During application startup, Spring scans the packages configured for component scanning, detects classes annotated with @Component, and automatically registers them as beans in the Spring container.
The @Bean annotation is used to explicitly define a bean in the Spring container. It is a method-level annotation and is typically used within a class annotated with @Configuration. The name of the @Bean method is used as the bean name by default, although it can be customized.
In summary, @Component is a class-level annotation used for automatic bean detection through component scanning, whereas @Bean is a method-level annotation used for explicit bean creation and configuration.
Example:-
@Component public class ProductUtil{ //methods }
@Configuration class HMSAppConfiguration{ @Bean public Customer getCustomer(){ return new Customer(); } }

No comments:
Post a Comment