Detailed explanation of Spring annotation Service

Service annotation

@Service Annotation is an annotation in Spring Framework, which identifies this class as a service bean of the business logic layer. This means that when the Spring application starts, the bean will be automatically created and added to the Spring application context.
In short, @Service annotation is an annotation used to mark service layer beans . It is one of the important methods to achieve business logic reuse in Spring Boot applications.

Service usage and examples

You can use the @Service annotation to declare a class as a business logic component and store its object in the Spring container. In the controller class, by injecting an instance of the component, you can call its methods.
The following is a code example for an order service that illustrates how to use the @Service annotation.
First, we define an order service interface, which contains two methods: one to create an order and one to query order details.

public interface OrderService {
    
    
    Order createOrder(Order order);
    Order getOrderDetails(String orderId);
}

Next, we implement the service interface and add the @Service annotation to the implementation class to mark it as a service layer bean.

@Service
public class OrderServiceImpl implements OrderService {
    
    

    @Override
    public Order createOrder(Order order) {
    
    
        // 在这里执行创建订单的业务逻辑
        return order;
    }

    @Override
    public Order getOrderDetails(String orderId) {
    
    
        // 在这里执行查询订单详情的业务逻辑
        return new Order();
    }
}
}

Finally, we can use the business logic in this service class in other components (such as Controller) by injecting instances of this service class. For example, the following code example shows how to inject and use the order service in the Controller component.

@RestController
public class OrderController {
    
    

    @Autowired
    private OrderService orderService;

    @PostMapping("/orders")
    public Order createOrder(@RequestBody Order order) {
    
    
        return orderService.createOrder(order);
    }

    @GetMapping("/orders/{orderId}")
    public Order getOrderDetails(@PathVariable String orderId) {
    
    
        return orderService.getOrderDetails(orderId);
    }
}

How is it done the traditional way?

public class OrderController {
    
    
    private OrderService orderService = new OrderServiceImpl();
    
    public Order createOrder(Order order) {
    
    
        return orderService.createOrder(order);
    }

    public Order getOrderDetails(String orderId) {
    
    
        return orderService.getOrderDetails(orderId);
    }
}

You can see that this method creates a service class instance directly in the controller component and calls the business logic in the service class in the controller component. So the shortcomings of this approach are readily apparent:
1. Manually create objects and manually write dependencies in the code.
2. Each class must explicitly create instances of the other classes it needs and reference these instances explicitly in the code.
3. The code may be full of tedious code for creating instances and referencing instances, making it difficult to maintain.
This approach is feasible when the code is simple and there are not a lot of dependencies, but in complex projects, the complexity of the code and the difficulty of maintaining the dependencies are often problems.

How does the @Service annotation reflect business logic reuse?

The following example shows how to use multiple instances of the OrderService class to reuse business logic in different places.
First, we inject an OrderService instance into another class PaymentService:

@Service
public class PaymentService {
    
    

    @Autowired
    private OrderService orderService;

    public void processPayment(long orderId) {
    
    
        // 调用订单服务以获取订单详细信息
        Order order = orderService.getOrderById(orderId);
        // 执行付款操作
        ...
    }
}

In this code, by injecting an instance of the OrderService class, we can use the getOrderById() method in the OrderService in the PaymentService class. In this way, we can reuse the business logic in OrderService in multiple different classes without having to implement it in each class .

Summarize

Service classes annotated with Service provide a way for other components to reuse business logic, which can reduce the writing of repeated code. You do not need to manually manage the creation and destruction of objects, nor do you need to manually maintain dependencies between objects. Improve code maintainability.
It is often used in conjunction with the controller component and the request processing component of the logic layer: inject the service class instance into the controller component, then use the instance to call the business logic of the service class, and return the result to the request processing component.

Guess you like

Origin blog.csdn.net/juggle_gap_horse/article/details/128972813