<aside> <img src="/icons/gradebook_purple.svg" alt="/icons/gradebook_purple.svg" width="40px" />

학습 목표

1. 의존성 제외와 대체 방법

Spring Boot Starter는 여러 하위 라이브러리를 포함하는 **메타 의존성(Meta Dependency)**이기 때문에, 불필요한 하위 라이브러리가 함께 따라오는 경우가 발생할 수 있다. Gradle에서는 exclude 블록을 활용하여 이러한 하위 라이브러리를 제외하거나, 원하는 구현체로 대체할 수 있다.

1-1. 특정 하위 라이브러리 제외하기

<aside> 💡

기본 로깅 구현체(Logback) 제거 예시

</aside>

Spring Boot는 기본적으로 spring-boot-starter-logging을 통해 SLF4J + Logback을 포함한다. 만약 이 로깅 시스템이 필요 없거나 Log4j2 등 다른 로깅 시스템을 사용하려면 이 기본 의존성을 제거해야 한다.

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}

1-2. 다른 구현체로 대체하기

<aside> 💡

기본으로 제공되는 구현체를 사용자 정의 라이브러리나 최신 버전으로 교체

</aside>

  1. 예: Jackson → Gson

    dependencies {
        implementation('org.springframework.boot:spring-boot-starter-web') {
            exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
        }
        implementation 'com.google.code.gson:gson:2.10.1'
    }
    

    참고로, 위에서 예시로 든 Jackson을 제외하면 Spring MVC의 기본 JSON 직렬화 기능이 사라지므로HttpMessageConverter 또는 WebMvcConfigurer를 통해 Gson 설정을 수동으로 적용해야 함.

  2. 예: Hibernate → EclipseLink

    JPA를 사용할 때, 기본 구현체(Hibernate)를 다른 구현체로 바꾸는 경우도 가능함

    dependencies {
        implementation('org.springframework.boot:spring-boot-starter-data-jpa') {
            exclude group: 'org.hibernate', module: 'hibernate-core'
        }
        im*plementation 'org.eclipse.persistence:eclipselink:3.0.2'
    }*
    

1-3. exclude의 한계와 주의사항

  1. 하위 의존성이 다시 끌고 오는 경우

    dependencies {
        // b 패키지에 y 의존성을 제거
        implementation('a:b') {
            exclude group: 'x', module: 'y'
        }
        // 만약 d 패키지에도 y 의존성이 존재한다면 다시 참조될 수 있으므로 중복 제거해줘야 함
        implementation('c:d') {
            exclude group: 'x', module: 'y'
        }
    }
    
  2. 애플리케이션 동작 중 오류 발생 가능성