I am using @Component, @Repository, @Service etc. in my current project. But I am still slight confuse with the uses of @Bean. Could anyone please clear my doubt that, When it is better to use @Component and when should We prefer @Bean?
Asked
Active
Viewed 147 times
0
-
1Possible duplicate of [Spring: @Component versus @Bean](http://stackoverflow.com/questions/10604298/spring-component-versus-bean) – Preuk May 26 '16 at 12:10
1 Answers
0
Basically @Component is the similiar concept to @Repository or @Service only more general. It enables for the class to be discovered during component scanning and marks the class as spring managed bean.
@Bean on the other hand is supposed to be used inside of @Configuration classes only. it is equivalent of <bean>
tag in xml configuration - you mark class as spring managed. The difference is that unlike @Component, it is not directly on the class (thus causing coupling) but in the configuration file.
Check this blogpost describing the difference between @Component an @Bean
http://zezutom.blogspot.com/2014/02/spring-series-part-5-component-vs-bean.html
Or this youtube video

Vojtech Ruzicka
- 16,384
- 15
- 63
- 66