Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 695 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the difference between @Component, @Repository & @Service annotations in Spring?

#11
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:

1. Help developers define a better category for the competent. This categorizing may become helpful in some cases. (For example when you are using `aspect-oriented`, these can be a good candidate for `pointcuts`)
2. `@Repository` annotation will add some functionality to your bean (some automatic exception translation to your bean persistence layer).
3. If you are using spring MVC, the `@RequestMapping` can only be added to classes which are annotated by `@Controller`.
Reply

#12
Spring provides four different types of auto component scan annotations, they are `@Component`, `@Service`, `@Repository` and `@Controller`. Technically, there is no difference between them, but every auto component scan annotation should be used for a special purpose and within the defined layer.

`@Component`: It is a basic auto component scan annotation, it indicates annotated class is an auto scan component.

`@Controller`: Annotated class indicates that it is a controller component, and mainly used at the presentation layer.

`@Service`: It indicates annotated class is a Service component in the business layer.

`@Repository`: You need to use this annotation within the persistence layer, this acts like database repository.

One should choose a more specialised form of `@Component` while annotating their class as this annotation may contain specific behavior going forward.
Reply

#13
In spring framework provides some special type of annotations,called stereotype annotations.
These are following:-

@RestController- Declare at controller level.
@Controller – Declare at controller level.
@Component – Declare at Bean/entity level.
@Repository – Declare at DAO level.
@Service – Declare at BO level.

above declared annotations are special because when we add `<context:component-scan>` into xxx-servlet.xml file ,spring will automatically create the object of those classes which are annotated with above annotation during context creation/loading phase.



Reply

#14
*Repository* and *Service* are children of *Component* annotation. So, all of them are *Component*. *Repository* and *Service* just expand it.
How exactly?
*Service* has only ideological difference: we use it for services. *Repository* has particular exception handler.
Reply

#15
As many of the answers already state what these annotations are used for, we'll here focus on some minor differences among them.

> First the **Similarity**
>
> First point worth highlighting again is that **with respect to scan-auto-detection and dependency injection for BeanDefinition** all these annotations (viz., @Component, @Service,
> @Repository, @Controller) are the same. **We can use one in place
> of another and can still get our way around.**


----------

## Differences between @Component, @Repository, @Controller and @Service ##

> **@Component**

This is a general-purpose stereotype annotation indicating that the class is a spring component.

***What’s special about @Component***
`<context:component-scan>` only scans `@Component` and does not look for `@Controller`, `@Service` and `@Repository` in general. They are scanned because they themselves are annotated with `@Component`.

Just take a look at `@Controller`, `@Service` and `@Repository` annotation definitions:

@Component
public @interface Service {
….
}

 

@Component
public @interface Repository {
….
}

 

@Component
public @interface Controller {

}


Thus, it’s not wrong to say that `@Controller`, `@Service` and `@Repository` are special types of `@Component` annotation. `<context:component-scan>` picks them up and registers their following classes as beans, just as if they were annotated with `@Component`.

Special type annotations are also scanned, because they themselves are annotated with `@Component` annotation, which means they are also `@Component`s. If we define our own custom annotation and annotate it with `@Component`, it will also get scanned with `<context:component-scan>`


----------


> **@Repository**

This is to indicate that the class defines a data repository.

***What’s special about @Repository?***

In addition to pointing out, that this is an *Annotation based Configuration*, `@Repository`’s job is to catch platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. For this, we’re provided with `PersistenceExceptionTranslationPostProcessor`, that we are required to add in our Spring’s application context like this:

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

This bean post processor adds an advisor to any bean that’s annotated with `@Repository` so that any platform-specific exceptions are caught and then re-thrown as one of Spring’s unchecked data access exceptions.


----------


> **@Controller**

The `@Controller` annotation indicates that a particular class serves the role of a controller. The `@Controller` annotation acts as a stereotype for the annotated class, indicating its role.

***What’s special about @Controller?***

We cannot switch this annotation with any other like `@Service` or `@Repository`, even though they look same.
The dispatcher scans the classes annotated with `@Controller` and detects methods annotated with `@RequestMapping` annotations within them. We can use `@RequestMapping` on/in only those methods whose classes are annotated with `@Controller` and it will **NOT** work with `@Component`, `@Service`, `@Repository` etc...

<sub>_Note: If a class is already registered as a bean through any alternate method, like through `@Bean` or through `@Component`, `@Service` etc... annotations, then `@RequestMapping` can be picked if the class is also annotated with `@RequestMapping` annotation. But that's a different scenario._</sub>


----------


> **@Service**

`@Service` beans hold the business logic and call methods in the repository layer.

***What’s special about @Service?***

Apart from the fact that it's used to indicate, that it's holding the business logic, there’s nothing else noticeable in this annotation; but who knows, Spring may add some additional exceptional in future.


----------


> ***What else?***

Similar to above, in the future Spring may add special functionalities for `@Service`, `@Controller` and `@Repository` based on their layering conventions. Hence, it's always a good idea to respect the convention and use it in line with layers.
Reply

#16
**Difference between @Component, @Repository, @Controller & @Service annotations**

> @Component – generic and can be used across application.<br> @Service
> – annotate classes at service layer level.<br> @Controller – annotate
> classes at presentation layers level, mainly used in Spring MVC.<br>
> @Repository – annotate classes at persistence layer, which will act as
> database repository.

`@Controller` = @Component ( Internal Annotation ) + Presentation layer Features <br>
`@Service` = @Component ( Internal Annotation ) + Service layer Features<br>
`@Component` = Actual Components ( Beans )<br>
`@Repository` = @Component ( Internal Annotation ) + Data Layer Features ( use for handling the Domain Beans )<br>
Reply

#17
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.

- To represent persistence layer components: `@Repository`
- To represent service layer components: `@Service`
- To represent presentation layer components: `@Controller`
- or else you can use `@Component` for all of them.
Reply

#18
**@Component**: you annotate a class `@Component`, it tells hibernate that it is a Bean.

**@Repository**: you annotate a class `@Repository`, it tells hibernate it is a DAO class and treat it as DAO class. Means it makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring `DataAccessException`.

**@Service**: This tells hibernate it is a Service class where you will have `@Transactional` etc Service layer annotations so hibernate treats it as a Service component.

Plus `@Service` is advance of `@Component`. Assume the bean class name is `CustomerService`, since you did not choose XML bean configuration way so you annotated the bean with `@Component` to indicate it as a Bean. So while getting the bean object `CustomerService cust = (CustomerService)context.getBean("customerService");` By default, Spring will lower case the first character of the component – from ‘CustomerService’ to ‘customerService’. And you can retrieve this component with name ‘customerService’.
But if you use `@Service` annotation for the bean class you can provide a specific bean name by

@Service("AAA")
public class CustomerService{
and you can get the bean object by

CustomerService cust = (CustomerService)context.getBean("AAA");

Reply

#19
**@Component** acts as @Bean annotation in configuration class , register bean in spring context. Also it is parent for @Service, @Repository and @Controller annotation.

**@Service**, extends @Component annotation and has only naming difference.

**@Repository** - extends @Component annotation and translate all database exceptions into
[DataAccessException][1].

**@Controller** - acts as controller in MVC pattern. The dispatcher will scan such annotated classes for mapped methods, detecting @RequestMapping annotations.


[1]:

[To see links please register here]

Reply

#20
`@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 controllers

Why 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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through