Backend/SpringBoot

[기본구조4] 서비스

Dean83 2024. 10. 23. 16:25

Repository Interface 를 컨트롤러 클래스에서 직접 제어 하는것은 보안측면에서 좋지 않다. 또한 코드 중복을 막기 위해서도 서비스 클래스를 이용하여 DB 처리 하는것이 좋다. 서비스 클래스를 따로 두면 엔티티를 DTO 적용하여 데이터 처리를 분리 할 수 있다.

 

  •  컨트롤러 클래스 -> 서비스 클래스 함수 호출 -> 서비스 클래스에서 Repository Interface 조작
  • @Service 어노테이션을 사용한다. 

 

package com.example.test1.test1

import org.springframework.stereotype.Service


//코틀린에서는 클래스 생성시 (안에 변수 선언을 바로 할 수 있다)
@Service
public class ServiceClass(val _repo : repo)
{
    public fun ReadItem() : List<entity1>
    {
        return _repo.findAll()
    }
}

 

컨트롤러 에서는 Repository 대신 서비스 클래스를 그대로 Injection 하여 사용하면 된다.