Adding a Service component
The Service
component is an interface that works between controllers and repositories and is where we'll add the business logic. Though you can directly call repositories from controllers, it is not a good practice as repositories should only be part of the data retrieval and persistence functionalities. Service components also help in sourcing data from various sources, such as databases and other external applications.
Service components are marked with the @Service
annotation, which is a specialized Spring @Component
that allows implemented classes to be auto-detected using class-path scanning. Service classes are used for adding business logic. Like Repository, the Service
object also represents both DDD's Service and Java EE's Business Service Façade pattern. Like Repository, it is also a general-purpose stereotype and can be used according to the underlying approach.
First we'll create the service interface, which is a...