January 13, 2021
Add Business Services

Spring Boot – Add Business Services

In this tutorial, we are going to add business services to our application.

  1. What is a Business Service
  2. How Business Services Work
  3. Add the User Service
  4. Add the Post Service
  5. Add the Location Service
  6. A Little Exercise for you

 

1. What is a business service?

I’m sure you will ask this question.

Well, a business service in Spring Framework is also known as service component. It is a class annotate with the @Service annotation. So, in this class, you place the business logic.

Although it is possible to place these logic in the Controller files, however it is best practice to put your business logic in another layer. So what happens is that the controller calls the business service each time a resource is required.

 

2. How Business Service Work

This is actually used to enforce dependency injection. So, when the application starts up, an instance of the business service is created and keeps it in memory.

Now, other classes and controllers would depend on this business service. So when another class needs an object,  the business service would provide it to that class without having to create a new object every time.  So when the application starts, spring would detect all the classes in the classpath with the @Service annotation. It would then create them and register them.

This is also why business services are known as singletons. Because they are created just once when the application starts.

 

3. Create the User Service

As mentioned before, a business service is a class annotated with the @Service annotation. So to create the user service, create a new class inside the user package. Name this class UserService.

Once created, you can then add the @Service annotation to the class. You will notice an error in the @Service annotation. Just place the mouse cursor in the @Service annotation and press Ctrl + Shift + O. This is a shortcut that  would add the neccessary import statement. You can also manually do the import.

If you completed this process, this is what the UserService.java file would contain

 

package user;

import org.springframework.stereotype.Service;

@Service
public class UserService {

}

 

 

4. Create the Post Service

Also go ahead to create the business service for the Post class. Call this service PostService. Once created also add the @Service annotation.

Then follow the same procedure and add the necessary imports. Below is the content of the PostService.java.

 

package post;

import org.springframework.stereotype.Service;

@Service
public class PostService {

}

 

 

5. Create the Location Service

Finally, create the LocationService. Follow exactly the same process.

At this point you have all your business services created. So in the next tutorial, we would test the business services and start adding controllers.

The content of the LocationService.java file is shown below

 

package location;

import org.springframework.stereotype.Service;

@Service
public class LocationService {

}

 

6. A Little Exercise for you

Notice that is @Service annotation is a stereotype annotation. Now this is an exercise for you.

  • What is a stereotype annotation?
  • What are the 4 other stereotype annotations in Spring

Leave your answers in the comment box below. The two correct responses would receive a prize from me!

0 0 vote
Article Rating
Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ankur Mukherjee
Ankur Mukherjee
1 year ago

@Component Indicates that an annotated class is a “component”
@Controller Indicates that an annotated class is a “Controller” (e.g.
@Service 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.”
@Repository Indicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”.

TONY J SEBASTIAN
TONY J SEBASTIAN
1 year ago

1. @Component Indicates that an annotated class is a “component” – It means that Spring framework will autodetect the classes for DI when annotation-based configuration and classpath scanning is used

2. @Controller Indicates that an annotated class is a “Controller” – It is to create a Map of model object and find a view

3. @Service Indicates that an annotated class is a “Service” – It is a specialization of the component annotation for service-layer classes

4. @Repository Indicates that an annotated class is a “Repository” – It is an annotation that marks the specific class as a Data Access Object

TONY J SEBASTIAN
TONY J SEBASTIAN
1 year ago

@Component Indicates that an annotated class is a “component” – It means that Spring framework will autodetect the classes for DI when annotation-based configuration and classpath scanning is used

@Controller Indicates that an annotated class is a “Controller” – It is to create a Map of model object and find a view

@Service Indicates that an annotated class is a “Service” – It is a specialization of the component annotation for service-layer classes

@Repository Indicates that an annotated class is a “Repository” – It is an annotation that marks the specific class as a Data Access Object

Fayza
Fayza
4 months ago

stereotype annotations in Spring are markers for any class that fulfills a role within an application.
Stereotype Annotations in Spring: @Component, @Repository, @Service and @Controller
Classes that are annotated with one of these annotations will automatically be registered in the Spring application context.
@Component: generic stereotype for any Spring-managed component

@Repository: stereotype for persistence layer,A class that serves in the persistence layer of the application as a data access object (DAO).
@Service: stereotype for service layer,ll your business logic should be in Service classes.
@Controller: stereotype for presentation layer (spring-mvc)

Giuliano
Giuliano
3 months ago

it is just to help the spring to detect where is its needs, where search some required action. Am I right?