-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
  • 439
  • 6
  • 10

2 Answers2

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();
    }

}
0

@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,263
  • 3
  • 30
  • 49