How do @Component, @Service, @Repository and @Bean differ? How is @Configuration different from @Component?
Short answer
@Service, @Repository, @Controller are @Component with a role, and scanning creates the bean. @Bean goes on a method, for classes you can't edit or that need custom setup; @Configuration gets a CGLIB proxy, so cross-calls between @Bean methods still return the singleton.
3 points to say:
@Service/@Repository/@Controllerare role-flavored@Components;@Repositorytranslates exceptions toDataAccessException.@Beanis for external classes or custom initialization.@Configurationis proxied to keep singletons on cross-method calls;@Beanmethods in a@Componentare lite mode, without a proxy.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.