• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

코틀린 코드로 변경해서 학습을 진행중이신 모든 분들께 질문이 있습니다.

23.03.24 00:41 작성 조회수 496

0

package deepboot.deep.config.autoconfig

import deepboot.deep.annotation.ConditionalMyOnClass
import deepboot.deep.annotation.MyAutoConfiguration
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.boot.web.servlet.server.ServletWebServerFactory
import org.springframework.context.annotation.Bean
import org.springframework.core.env.Environment

@MyAutoConfiguration
@ConditionalMyOnClass("org.apache.catalina.startup.Tomcat")
class TomcatWebServerConfig() {

    @Value(value = "\${context.path}")
    lateinit var contextPath: String

    @Bean("tomcatWebServerFactory")
    @ConditionalOnMissingBean
    fun servletWebServerFactory(env: Environment): ServletWebServerFactory {
        val factory = TomcatServletWebServerFactory()
        println("contextPath: $contextPath")
        factory.contextPath = this.contextPath
        return factory
    }
}

 

현재 제 TomcatWebServerConfig() 전체 코드입니다.

contextPath 를 출력했을 때 결과가

contextPath: ${context.path}

위 처럼 나오고, IllegalArgumentException 이 발생합니다.

 

ContextPath must start with '/' and not end with '/'

제가 생각했을 때 이 @Value 를 제대로 못 읽어오는 것 같은데, 코틀린 클래스 생성자 파라미터로 contextPath 를 주입해도, lateinit var 로 선언해도 여전히 properties 에서 값을 못 읽어오네요.

 

이런 동일 증상 겪으신 분 어떻게 해결했는지 궁금합니다.

아래는 전체 코드 깃허브 주소입니다.

https://github.com/dailyzett/deep

도와주시면 정말 감사하겠습니다.

chatGPT 로도 계속 물어보고 있는데 제자리만 돌고 있는 느낌이네요.

답변 1

답변을 작성해보세요.

2

github에 올라온 코드를 살펴봤습니다.

현재 코드에서 @Value가 적용이 안 되고 에러가 나는 게 정상입니다. 강의의 해당 부분에서도 ${}를 못 읽어오는 단계를 보여주고 이유를 설명합니다. 이걸 해결하기 위해서 어떤 작업이 필요한지 바로 이어서 다룹니다. 좀 더 강의를 따라가보세요.

그리고 더 중요한 문제가 보입니다. 이번 에러와는 상관은 없지만요.

강의 예제의 패키지 구조는 config과 boot 파트를 완전히 분리합니다. Application 클래스(만드신 예제에서는 DeepApplication)의 하위 폴더에 config을 두시면 "절대" 안됩니다. 물론 한동안 동작은 잘 되는 것처럼 보입니다. 하지만 후반부 조건부 자동구성을 비롯한 중요한 단계에서 제대로 동작하지 않게됩니다.

config폴더는 deepboot.deep 패키지 아래가 아니라, deepboot아래로 옮기셔야 합니다. 안그러면 강의 예제와 같은 결과를 보실 수 없습니다.

더 해보시고 또 해결되지 않는 문제가 보이면 이번처럼 프로젝트를 공유해주시면 원인을 찾아보겠습니다.

김세준님의 프로필

김세준

질문자

2023.03.24

자세한 답변 정말 감사드립니다!!