Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use service or component in spring? [duplicate]

When to use service or component in spring?

For example, is a module responsible for sending email or common business logic a "service" or a "component"? and what's the difference?

Is a service able to call the other services? Is there any transaction problem? or a service should call the components only?

Someone told me that a service should never call the other services and should only call the components instead, which means Controller->Service->Component->DAO, but I found many people share the concept of Controller->Service->DAO with no component.

Is there any system design criteria about this topic in Spring?

like image 287
ChiaChih wu Avatar asked Sep 18 '17 05:09

ChiaChih wu


People also ask

What happens if we use @component instead of service?

We can use @Component across the application to mark the beans as Spring's managed components. Spring will only pick up and register beans with @Component, and doesn't look for @Service and @Repository in general. @Service and @Repository are special cases of @Component.

What is the difference between @component and @service?

@Component is a generic stereotype for any Spring-managed component or bean. @Repository is a stereotype for the persistence layer. @Service is a stereotype for the service layer.

Can we @component instead of @service?

If your component is a generic component, not really living at the service layer, or is accessible but could hold state, then use @Component . If your component is a specific service, living at the service layer, or is accessible and does not inherently hold state, use @Service .

Why would you recommend changing the component annotation to service?

It is a specialization of @Component Annotation. One most important thing about the @Service Annotation is it can be applied only to classes. It is used to mark the class as a service provider. So overall @Service annotation is used with classes that provide some business functionalities.


1 Answers

In order to "configure" Spring so it can provide you with the instances of the classes you need, you are supposed to tell Spring what objects are involved and how they are built. To do this you can use an xml configuration file or through annotations

In case you take the annotation approach (IMHO a much better and simpler one) you can use @Component to annotate the class. This is like telling Spring: "Hey! I want you to know that you may need an instance of this class. Maybe because I request it, maybe because something I requested needs it". So annotating a class with @Component just let Spring know that it exists

There are other annotations that do the same:

  • @Controller (and @RestController)
  • @Service
  • @Repository

They all inform Spring that the class is involved in the DI context. But they also have semantic meaning:

  • @Controller = @Component belonging to Presentation Layer
  • @Service = @Component belonging to Service/Use Case Layer
  • @Repository = @Component belonging to Persistence Layer

You can find more info in this question

Should a service be able to call the other services?

I don't see any problem with that. If any of your services requires to do some actions that are already performed by other you surely want to avoid code duplication. As long as you respect the architecture layers dependency (never going up) you'll be fine.

About this you can check this article about Clean Architecture

like image 86
Alberto S. Avatar answered Sep 23 '22 16:09

Alberto S.