@Component, @Repository and @Service Annotations in Spring

If some classes are annotated with @Component,@Repsoitory or @Service annotations then spring IOC container would create beans for those classes. The package in which those classes reside should be enabled for compoent scan.

This is the first importance for the existence of these annotations.

Now let’s see in details whta these annotations are:

@Component: It is a generic stereotype for any Spring-managed component. Spring would search for classes annoatated with @component annotation and creates the beans. They are registered under application context as well for bean creation.

@Component
class EmployeeUtility{
....
...
}

@Repository
class EmployeeDAOImpl{
....
...
}

@Service
class EmployeeService{
....
...
}

@Repository:Classes are annotated with Repository annotation to signify that the class is kind of DAO class which connects or you can say interacts databases. By annotating a class with this annoation, the class is registered to throw some database or persistence specific exceptions and throw them as spring’s unchecked exceptions.Repository is a type of Component as well. So it is treated similarly as Component.

@Service: Classes which are annotated with @Service annotation are generally service layer classes. It is a type of Compoent as well. So it is treated similarly as Component.