I am little bit confused about the difference between a service and component. Can someone explain with example that what is the difference between a service and component?
That is to say that a service can be deployed separately from the rest of your systems and applications. Services are designed to maximize reuse as opposed to being designed to fit a particular system or application. Components are parts of a system or application that are designed to work together.
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 .
If we use @Component instead of @Service, it becomes more challenging to identify which @Component actually has the service methods – since we may have many other @Component Classes. This means that our AOP Pointcut has to be more specific which introduces some potential issues.
@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. @Controller is a stereotype for the presentation layer (spring-MVC).
A service can be made up of several components. Usually a service provides one complete feature that is made up by combining different components.
The service's user don't need to know anything about the underlying components. User will deal only directly with the service while service internally will be interacting with the components
Services are applications that are (generally) designed to be long running, tied to the operations of the system rather than a user and provide a utility to other applications. Databases, SMTP, Active Directory are all examples of "Services".
Components are pre-formed pieces that can be included in other applications and are not designed to operate 'on their own'. An application that references a database my use a data component (SQLClient) to communicate with a date base service (MS SQL Server).
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