- @Component auto detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean, rather than letting Spring do it automatically.
- @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.
- @Component is a class level annotation where as @Bean is a method level annotation and name of the method serves as the bean name.
- @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.
- 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.
- @Component has different specializations like @Controller, @Repository and @Service whereas @Bean has no specializations.
1. component : 1 利用spring 類路徑掃描,spring
2. 寫在類上,和類定義不分離。 ide
3. 不須要配合 @configuration註解spa
2. Bean : .當類不受spring控制的時候,寫一個方法,返回某個不受spring控制的類的對象。 這樣就能在spring裏注入一個bean。component
配合configuration註解對象
參考:https://stackoverflow.com/questions/10604298/spring-component-versus-bean ci