@Service

존스노우·2024년 5월 22일
0

기타

목록 보기
8/9

@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."
May also indicate that a class is a "Business Service Facade" (in the Core J2EE patterns sense), or something similar. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

출처: Spring Framework Documentation - @Service

번역

  • 에릭 에반스(2003)의 도메인 주도 설계(Domain-Driven Design)에서 "캡슐화된 상태 없이 모델에서 독립적으로 제공되는 인터페이스로서의 작업"으로 정의

  • 비즈니스 서비스 퍼사드"(코어 J2EE 패턴의 의미에서)나 이와 유사한 것임을 나타낼 수 있다.

  • 대략 이정도.. 중요한 건

역할?

  • 비즈니스 로직 구현 : 핵심 기능 규칙 구현

  • 트랜잭션 관리 : @Transactional 어노테이션 사용해서 관리

  • 다른 서비스 및 리포지토리 호출해 작업 위임

  • 예외처리 등..

  • 출처: Spring - The Service Layer

퍼사드? 퍼사드 패턴? (비즈니스 서비스 퍼사드)


@Service
public class OrderServiceFacade {
    private final OrderRepository orderRepository;
    private final PaymentService paymentService;
    private final InventoryService inventoryService;
    private final NotificationService notificationService;

    // 생성자 주입
    public OrderServiceFacade(OrderRepository orderRepository, 
                              PaymentService paymentService,
                              InventoryService inventoryService, 
                              NotificationService notificationService) {
        this.orderRepository = orderRepository;
        this.paymentService = paymentService;
        this.inventoryService = inventoryService;
        this.notificationService = notificationService;
    }

    @Transactional
    public Order placeOrder(OrderRequest orderRequest) {
        // 재고 확인
        inventoryService.checkAvailability(orderRequest.getItems());

        // 주문 생성
        Order order = new Order(orderRequest.getItems(), orderRequest.getShippingAddress());
        orderRepository.save(order);

        // 결제 처리
        paymentService.processPayment(order);

        // 알림 발송
        notificationService.sendOrderConfirmation(order);

        return order;
    }

    // 다른 비즈니스 로직 메서드...
}
  • 주문 생성이라는 복잡한 프로세스를 캡슐화
  • 퍼사드 패턴을 사용하면 복잡한 서브시스템을 간단한 인터페이스로 추상화할 수 있으며, 클라이언트와 서브시스템 간의 결합도를 낮출 수 있음.
profile
어제의 나보다 한걸음 더

0개의 댓글

관련 채용 정보