-1

Component is used in class level definition by @Component annotation where Bean is used in construction or method level definition by @Bean annotation. @Component are used to auto-detect and auto-configure beans using classpath scanning. What does that mean?

Md. Asaduzzaman
  • 799
  • 1
  • 9
  • 12

2 Answers2

1

@Component can be used for , the spring to automatically find the bean and register to the context.

@Bean - Its our responsibility to provide the instantiation implementation for the particular bean.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
0

Both annotations are used for defining Spring managed beans.

You use @Component to define a bean outside of a @Configuration. You apply the annotation on top of the class that defines the component.

@Component
public class MyComponent {

}

You use @Bean to define a bean within a @Configuration. You apply the annotation on top of the method that creates the bean.

@Configuration
public class MyConfig {

    @Bean
    public MyComponent myComponent() {
        return new MyComponent();
    }

}