0
0
Spring Bootframework~5 mins

Business logic in services in Spring Boot

Choose your learning style9 modes available
Introduction

Business logic in services means putting the main rules and decisions of your app in special parts called services. This keeps your code clean and easy to change.

When you want to keep your controller code simple and focused on handling web requests.
When you need to reuse the same business rules in different parts of your app.
When you want to separate how data is stored from how your app works.
When you want to test your app's main logic without dealing with web or database details.
When your app grows and you want to keep it organized and easy to maintain.
Syntax
Spring Boot
public class SomeService {
    // Business logic methods
    public ReturnType methodName(Parameters) {
        // rules and decisions here
    }
}

Services are usually marked with @Service annotation in Spring Boot.

Services are called by controllers or other parts of the app to perform tasks.

Examples
This service calculates the total price of an order by adding up item prices times quantities.
Spring Boot
@Service
public class OrderService {
    public double calculateTotalPrice(Order order) {
        double total = 0;
        for (Item item : order.getItems()) {
            total += item.getPrice() * item.getQuantity();
        }
        return total;
    }
}
This service checks if a user is active and has the admin role to allow access.
Spring Boot
@Service
public class UserService {
    public boolean canUserAccessFeature(User user) {
        return user.isActive() && user.getRole().equals("ADMIN");
    }
}
Sample Program

This example shows a CalculatorService with business logic for adding and multiplying numbers. The CalculatorController calls this service to handle web requests. This keeps the logic separate from the web code.

Spring Boot
package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class CalculatorService {
    public int add(int a, int b) {
        return a + b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }
}

package com.example.demo.controller;

import com.example.demo.service.CalculatorService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CalculatorController {

    private final CalculatorService calculatorService;

    public CalculatorController(CalculatorService calculatorService) {
        this.calculatorService = calculatorService;
    }

    @GetMapping("/add")
    public int add(@RequestParam int a, @RequestParam int b) {
        return calculatorService.add(a, b);
    }

    @GetMapping("/multiply")
    public int multiply(@RequestParam int a, @RequestParam int b) {
        return calculatorService.multiply(a, b);
    }
}
OutputSuccess
Important Notes

Keep business logic in services to make your app easier to test and maintain.

Controllers should only handle web requests and responses, not complex logic.

Use dependency injection to get service instances in controllers.

Summary

Business logic belongs in services, not controllers or repositories.

Services help keep code organized and reusable.

Spring Boot uses @Service to mark service classes.