inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

토비의 스프링 부트 - 이해와 원리

@Value와 PropertySourcesPlaceholderConfigurer

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

해결된 질문

666

김세준

작성한 질문수 5

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 로도 계속 물어보고 있는데 제자리만 돌고 있는 느낌이네요.

spring spring-boot kotlin

답변 1

2

토비

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

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

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

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

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

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

0

김세준

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

11강에서 cmd에서 spring shell에 $ init 하면 Fail 메세지

0

77

2

TestRestTemplate 을 통해 테스트 실행시 웹 요청 정보가 콘솔에 표시되지 않습니다.

0

86

1

섹션7. 자동구성 정보파일분리 강의 질문(@MyAutoConfiguration 붙힌 이유)

0

202

2

WebApplicationContext를 DispatcherServlet에 this로 넘기는 것

0

279

2

인프라 빈 구성 정보의 분리에서 EnableMyAutoConfiguration 질문드립니다.

0

211

2

질문드립니다.

0

234

2

spring boot 3.3.7로 학습중입니다.

0

372

2

Serverproperties 객체 생성 후 @Impor 어노테이션 사용 이유 용도

0

162

2

spring start io 에서 이제더이상 2.x버전은 지원하지 않는 것 같습니다.

1

299

2

Springboot 3.2 이상에서 파라미터 추론관련

0

920

4

binding error

0

223

3

Arrays.copyOf 메서드의 타입 세이프

1

156

2

MyOnClassCondition에 있는 matches method의 Invoke 횟수

1

234

3

인용구의 출처가 궁금합니다.

0

260

1

프로퍼티 빈의 후처리기 도입 AnnotationUtils의 사용

0

236

2

SimpleCacheConfiguration과 빈 등록

0

169

2

MyAutoConfigImportSelector 에서 생성자로 ClassLoader를 주입받을 수 있는 점

0

244

1

IntelliJ project jenerator spring initailizr

0

150

1

강의 자료 레퍼지토리에 업로드

0

216

1

강의자료

0

388

1

Hikari 라이브러리가 없으면 오류가 나는거 아닌가요

0

315

2

Tomcat 포트 프로퍼티 미설정시 랜덤 포트 설정 문의

0

477

5

@Import 로 Bean을 등록해야하는 기준이 뭔지 궁금합니다.

0

344

2

application.properties파일내 프로퍼티 이름

0

214

1