DEV Community

sunj
sunj

Posted on

Spring Boot @Service, 2024-03-07

service.ApiServiceImpl.java

import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ApiServiceImpl implements ApiService {
Enter fullscreen mode Exit fullscreen mode

Service는 Client의 요청에 대한 비즈니스 로직을 수행하는 Component이다. Client의 요청 url에 따른 Controller가 호출되면, Controller는 해당 비즈니스 로직을 수행하기 위한 Service를 호출한다. Service는 로직 수행 후 결과를 Controller에게 반환한다.

service.ApiService.java

public interface ApiService {
Enter fullscreen mode Exit fullscreen mode

Implement를 통해 비즈니스 로직을 구현한다.
Interface를 사용하면 클래스간의 결합이 느슨해진다.

  1. 비즈니스 로직이 바뀌었을 경우 Implement만 갈아끼우면 된다. (코드 수정 1줄)

  2. 캐시를 사용 할 수 있게 된다.
    내부 캐시를 통해 내부에 데이터가 존재하지 않는 경우에만 메소드가 실행 하도록 할 수 있다.

참조 : https://jaeano.tistory.com/entry/Spring-Boot-Service-Component-Service-RequiredArgsConstructor

Top comments (0)