Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?
@Component
@Controller
@Repository
@Service
@RestController
These are all StereoType annotations.this are usefull for the making our classes as spring beans in ioc container,
There is no difference between @Component
, @Service
, @Controller
, @Repository
.
@Component
is the Generic annotation to represent the component of our MVC. But there will be several components as part of our MVC application like service layer components, persistence layer components and presentation layer components. So to differentiate them Spring people have given the other three annotations also.
@Repository
@Service
@Controller
@Component
for all of them.@Component
, @ Repository
, @ Service
, @Controller
:
@Component
is a generic stereotype for the components managed by Spring @Repository
, @Service
, and @Controller
are @Component
specializations for more specific uses:
@Repository
for persistence@Service
for services and transactions@Controller
for MVC controllersWhy use @Repository
, @Service
, @Controller
over @Component
?
We can mark our component classes with @Component, but if instead we use the alternative that adapts to the expected functionality. Our classes are better suited to the functionality expected in each particular case.
A class annotated with @Repository
has a better translation and readable error handling with org.springframework.dao.DataAccessException. Ideal for implementing components that access data (DataAccessObject or DAO).
An annotated class with @Controller
plays a controller role in a Spring Web MVC application
An annotated class with @Service
plays a role in business logic services, example Facade pattern for DAO Manager (Facade) and transaction handling
They are almost the same - all of them mean that the class is a Spring bean. @Service
, @Repository
and @Controller
are specialized @Component
s. You can choose to perform specific actions with them. For example:
@Controller
beans are used by spring-mvc@Repository
beans are eligible for persistence exception translationAnother thing is that you designate the components semantically to different layers.
One thing that @Component
offers is that you can annotate other annotations with it, and then use them the same way as @Service
.
For example recently I made:
@Component
@Scope("prototype")
public @interface ScheduledJob {..}
So all classes annotated with @ScheduledJob
are spring beans and in addition to that are registered as quartz jobs. You just have to provide code that handles the specific annotation.
We can answer this according to java standard
Referring to JSR-330
, which is now supported by spring, you can only use @Named
to define a bean (Somehow @Named=@Component
). So according to this standard, there seems that there is no use to define stereotypes (like @Repository
, @Service
, @Controller
) to categories beans.
But spring user these different annotations in different for the specific use, for example:
aspect-oriented
, these can be a good candidate for pointcuts
)@Repository
annotation will add some functionality to your bean (some automatic exception translation to your bean persistence layer).@RequestMapping
can only be added to classes which are annotated by @Controller
.Good enough answers are here to explain the whats-the-difference-between-component-repository-service-annotations. I would like to share the difference between @Controller & @RestController
@Controller
vs RestController
@RestController
:@Controller
which adds
@Controller
and @ResponseBody
annotation automatically. so we do not have to add @ResponseBody
to our mapping methods. That means
@ResponseBody
is default active.@RestController
you cannot return a view (By using
Viewresolver
in Spring/Spring-Boot)@RestController
also converts the response to JSON/XML automatically
as @ResponseBody
makes the returned objects to something that could be in the body, e.g. JSON or XML
@Controller
@Controller
is used to mark classes as Spring MVC Controller. This
annotation is just a specialized version of @Component
and it
allows the controller classes to be auto-detected based on classpath
scanning.@Controller
you can return a view in Spring web MVC.More Detailed View