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?

#1
Can [`@Component`][1], [`@Repository`][2] and [`@Service`][3] annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

In other words, if I have a Service class and I change the annotation from `@Service` to `@Component`, will it still behave the same way?

Or does the annotation also influence the behavior and functionality of the class?


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#2
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 translation

Another 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.
Reply

#3
Use of `@Service` and `@Repository` annotations are important from database connection perspective.

1. Use `@Service` for all your web service type of DB connections
2. Use `@Repository` for all your stored proc DB connections

If you do not use the proper annotations, you may face commit exceptions overridden by rollback transactions. You will see exceptions during stress load test that is related to roll back JDBC transactions.
Reply

#4
**@Repository** **@Service** and **@Controller** are serves as specialization of @Component for more specific use on that basis you can replace @Service to @Component but in this case you loose the specialization.

1. **@Repository** - Automatic exception translation in your persistence layer.
2. **@Service** - It indicates that the annotated class is providing a business service to other layers within the application.
Reply

#5
In Spring 4, latest version:

> The @Repository annotation is a marker for any class that fulfills the
> role or stereotype of a repository (also known as Data Access Object
> or DAO). Among the uses of this marker is the automatic translation of
> exceptions as described in Section 20.2.2, “Exception translation”.
>
> Spring provides further stereotype annotations: @Component, @Service,
> and @Controller. @Component is a generic stereotype for any
> Spring-managed component. @Repository, @Service, and @Controller are
> specializations of @Component for more specific use cases, for
> example, in the persistence, service, and presentation layers,
> respectively. Therefore, you can annotate your component classes with
> @Component, but by annotating them with @Repository, @Service, or
> @Controller instead, your classes are more properly suited for
> processing by tools or associating with aspects. For example, these
> stereotype annotations make ideal targets for pointcuts. It is also
> possible that @Repository, @Service, and @Controller may carry
> additional semantics in future releases of the Spring Framework. Thus,
> if you are choosing between using @Component or @Service for your
> service layer, @Service is clearly the better choice. Similarly, as
> stated above, @Repository is already supported as a marker for
> automatic exception translation in your persistence layer.
Reply

#6
A `@Service` to quote spring documentation,

> Indicates that an annotated class is a "Service", **originally defined
> by Domain-Driven Design (Evans, 2003) as "an operation offered as an
> interface that stands alone in the model, with no encapsulated state."**
> May also indicate that a class is a "Business Service Facade" (in the
> Core J2EE patterns sense), or something similar. This annotation is a
> general-purpose stereotype and individual teams may narrow their
> semantics and use as appropriate.

If you look at domain driven design by eric evans,

> A SERVICE is an operation offered as an interface that stands alone in
> the model, without encapsulating state, as ENTITIES and VALUE OBJECTS
> do. SERVICES are a common pattern in technical frameworks, but they
> can also apply in the domain layer. The name service emphasizes the
> relationship with other objects. Unlike ENTITIES and VALUE OBJECTS, it
> is defined purely in terms of what it can do for a client. A SERVICE
> tends to be named for an activity, rather than an entity—a verb rather
> than a noun. A SERVICE can still have an abstract, intentional
> definition; it just has a different flavor than the definition of an
> object. A SERVICE should still have a defined responsibility, and that
> responsibility and the interface fulfilling it should be defined as
> part of the domain model. Operation names should come from the
> UBIQUITOUS LANGUAGE or be introduced into it. Parameters and results
> should be domain objects. SERVICES should be used judiciously and not
> allowed to strip the ENTITIES and VALUE OBJECTS of all their behavior.
> But when an operation is actually an important domain concept, a
> SERVICE forms a natural part of a MODEL-DRIVEN DESIGN. Declared in the
> model as a SERVICE, rather than as a phony object that doesn't
> actually represent anything, the standalone operation will not mislead
> anyone.

and a `Repository` as per Eric Evans,

> A REPOSITORY represents all objects of a certain type as a conceptual
> set (usually emulated). It acts like a collection, except with more
> elaborate querying capability. Objects of the appropriate type are
> added and removed, and the machinery behind the REPOSITORY inserts
> them or deletes them from the database. This definition gathers a
> cohesive set of responsibilities for providing access to the roots of
> AGGREGATES from early life cycle through the end.
Reply

#7
all these annotations are type of stereo type type of annotation,the difference between these three annotations are

> - If we add the @Component then it tells the role of class is a component class it means it is a class consisting some logic,but it
> does not tell whether a class containing a specifically business or
> persistence or controller logic so we don't use directly this
> @Component annotation
> - If we add @Service annotation then it tells that a role of class consisting business logic
> - If we add @Repository on top of class then it tells that a class consisting persistence logic
> - Here @Component is a base annotation for @Service,@Repository and @Controller annotations

for example

package com.spring.anno;
@Service
public class TestBean
{
public void m1()
{
//business code
}
}

package com.spring.anno;
@Repository
public class TestBean
{
public void update()
{
//persistence code
}
}

- whenever we adds the `@Service` or `@Repositroy` or `@Controller` annotation by default `@Component` annotation is going to existence on top of the class





Reply

#8
**Explanation of stereotypes :**

- `@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 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.
- `@Autowired` - Let Spring auto-wire other beans into your classes using @Autowired annotation.

`@Component` is a generic stereotype for any Spring-managed component. `@Repository`, `@Service`, and `@Controller` are specializations of `@Component` for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Originally answered [here][1].


[1]:

[To see links please register here]

Reply

#9
> Spring 2.5 introduces further stereotype annotations: @Component, @Service and @Controller. @Component serves as a generic stereotype for any Spring-managed component; whereas, @Repository, @Service, and @Controller serve as specializations of @Component for more specific use cases (e.g., in the persistence, service, and presentation layers, respectively). What this means is that you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts. Of course, it is also possible that @Repository, @Service, and @Controller may carry additional semantics in future releases of the Spring Framework. Thus, if you are making a decision between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

> @Component – Indicates a auto scan component.
@Repository – Indicates DAO component in the persistence layer.
@Service – Indicates a Service component in the business layer.
@Controller – Indicates a controller component in the presentation layer.


reference :- [Spring Documentation - Classpath scanning, managed components and writing configurations using Java ][1]


[1]:

[To see links please register here]

Reply

#10
In Spring `@Component`, `@Service`, `@Controller`, and `@Repository` are Stereotype annotations which are used for:

`@Controller:` where your **request** **mapping from presentation page** done i.e. Presentation layer won't go to any other file it goes directly to `@Controller` class and checks for requested path in `@RequestMapping` annotation which written before method calls if necessary.

`@Service`: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user not directly call persistence method so it will call this method using this annotation. **It will request @Repository as per user request**

`@Repository`: This is Persistence layer(Data Access Layer) of application which used to get data from the database. i.e. **all the Database related operations are done by the repository.**

`@Component` - Annotate your other components (for example REST resource classes) with a component stereotype.

> Indicates that an annotated class is a "[component][1]". Such classes are
> considered as candidates for auto-detection when using
> annotation-based configuration and classpath scanning.
>
> Other class-level annotations may be considered as identifying a
> component as well, typically a special kind of component: e.g. the
> @Repository annotation or AspectJ's @Aspect annotation.



[![enter image description here][2]][1]


[1]:

[To see links please register here]

[2]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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