When we are going to develop any Project it should be lossy coupled and maintainable
. To achieve this layer separation is important
@Service
- Annotate all your service classes with @Service. This layer knows the unit of work. All your business logic will be in Service classes. Generally, methods of service layer are covered under a transaction. You can make multiple DAO calls from service method. if one transaction fails all transactions should rollback.
@Repository
- Annotate all your DAO classes with @Repository. All your database access logic should be in DAO classes.
@Component
- Annotate your other components (for example REST resource classes) with component stereotype.
Reasons to use them :
- The main advantage of using @Repository or @Service over @Component
is that it's easy to write an AOP pointcut that targets, for
instance, all classes annotated with @Repository.
- You don't have to write bean definitions in context xml file. Instead
annotate classes and use those by autowiring.
- Specialized annotations help to clearly demarcate application layers
(in a standard 3 tiers application).
What is Stereotype Refer Here
@Component generic stereotype for any Spring-managed component
@Repository stereotype for persistence layer
@Service stereotype for service layer
@Controller stereotype for presentation layer (spring-mvc)
For More Details Click Here and Here