And @ Component of difference @Bean

  1. @Component auto detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean, rather than letting Spring do it automatically.
  2. @Component does not decouple the declaration of the bean from the class definition where as @Bean decouples the declaration of the bean from the class definition.
  3. @Component is a class level annotation where as @Bean is a method level annotation and name of the method serves as the bean name.
  4. @Component need not to be used with the @Configuration annotation where as @Bean annotation has to be used within the class which is annotated with @Configuration.
  5. We cannot create a bean of a class using @Component, if the class is outside spring container whereas we can create a bean of a class using @Bean even if the class is present outside the spring container.
  6. @Component has different specializations like @Controller, @Repository and @Service whereas @Bean has no specializations.

 

1. component: 1 using a spring-based scanning path,

        2. Write on the class, the class definition, and not separated. 

        3. No notes with @configuration

2. Bean:. When the class is not spring controlled, write a method that returns an object from a spring control class. This will inject a bean in the spring.

      With configuration notes

 Reference: https: //stackoverflow.com/questions/10604298/spring-component-versus-bean       

Guess you like

Origin www.cnblogs.com/lhuser/p/10972833.html