Spring Boot – Add Business Services

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;

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;

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;

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!