Loading...
Spring Framework Reference Documentation 7.0.2의 Context Configuration with Context Customizers의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
ContextCustomizer는 bean 정의들이 context에 로딩된 이후이지만 context가 refresh되기 전 사이에 제공된 ConfigurableApplicationContext를 커스터마이징하는 역할을 합니다.
ContextCustomizerFactory는 주어진 test class에 대해 ContextCustomizer가 필요한지를 결정하는 특정 어노테이션의 존재 여부와 같은 사용자 정의 로직에 기반하여 ContextCustomizer를 생성하는 역할을 합니다. Factory들은 test class에 대한 context 설정 attribute들이 ContextLoaders에 의해 처리된 이후이지만 MergedContextConfiguration이 생성되기 전 사이에 호출됩니다.
예를 들어, Spring Framework는 기본적으로 등록되는 다음과 같은 ContextCustomizerFactory 구현을 제공합니다:
MockServerContainerContextCustomizerFactory
Classpath에 WebSocket 지원이 존재하고 test class 또는 이를 둘러싼 enclosing class들 중 하나가 @WebAppConfiguration으로 어노테이션 또는 메타어노테이션 되어 있는 경우 MockServerContainerContextCustomizer를 생성합니다. MockServerContainerContextCustomizer는 새로운 MockServerContainer를 인스턴스화하고 이를 ServletContext에 jakarta.websocket.server.ServerContainer라는 이름의 attribute로 저장합니다.ContextCustomizerFactory Implementations@ContextCustomizerFactories 어노테이션을 사용하여 test class, 그 subclass 및 nested class들에 대해 ContextCustomizerFactory 구현들을 명시적으로 등록할 수 있습니다. 자세한 내용과 예시는
annotation support
와
@ContextCustomizerFactories
javadoc을 참고하십시오.
ContextCustomizerFactory Implementations@ContextCustomizerFactories를 사용하여 ContextCustomizerFactory 구현들을 등록하는 방식은 제한된 testing 시나리오에서 사용되는 커스텀 factory에 적합합니다. 그러나 커스텀 factory를 전체 test suite 전반에 걸쳐 사용해야 하는 경우에는 번거로울 수 있습니다.
이 문제는 SpringFactoriesLoader 메커니즘을 통한 기본 ContextCustomizerFactory 구현들의 자동 discovery 지원을 통해 해결됩니다.
예를 들어, Spring Framework와 Spring Boot의 testing 지원을 구성하는 모듈들은 모든 core 기본 ContextCustomizerFactory 구현들을 자신의 META-INF/spring.factories properties 파일 내의 org.springframework.test.context.ContextCustomizerFactory key 아래에 선언합니다. spring-test 모듈에 대한 spring.factories 파일은
여기에서 볼 수 있습니다. 서드파티 framework와 개발자들은 자신들의 spring.factories 파일을 통해 동일한 방식으로 기본 factory 목록에 자신들의 ContextCustomizerFactory 구현들을 추가할 수 있습니다.
ContextCustomizerFactory Implementations커스텀 ContextCustomizerFactory가 @ContextCustomizerFactories를 통해 등록된 경우, 이는 앞서 언급한
automatic discovery mechanism
을 사용하여 등록된 기본 factory들과 merge 됩니다.
Merging 알고리즘은 목록에서 중복을 제거하고, merge 시 로컬에서 선언된 factory들이 기본 factory 목록의 뒤에 이어 붙여지도록 보장합니다.
Test class, 그 subclass 및 nested class들에 대한 기본 factory들을 교체하려면,
@ContextCustomizerFactories의mergeModeattribute를MergeMode.REPLACE_DEFAULTS로 설정하면 됩니다.
Mixing XML, Groovy Scripts, and Component Classes
Context Configuration with Context Initializers