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?
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.
@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.
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 .
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.
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 LayerYou 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With