보다시피 foodProviderService 빈을 @Bean 애너테이션으로 정의했습니다. 이 클래스는 @Profile 애너테이션을 적용해 kindergarten 프로파일 전용으로 마킹됐습니다. highschool 프로파일도 프로파일 이름과 구성 클래스 이름을 제외하면 완벽히 동일합니다.
예제 4-78 고등학교 프로파일 구성 클래스([[profile]] HighschoolConfig.java)
package com.apress.prospring5.ch4.config; import com.apress.prospring5.ch4.FoodProviderService; import com.apress.prospring5.ch4.highschool.FoodProviderServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("highschool") public class HighschoolConfig { @Bean public FoodProviderService foodProviderService(){ return new FoodProviderServiceImpl(); } }
이 클래스는 XML 파일과 마찬가지 방식으로 사용됩니다. 컨텍스트에는 양쪽 모두를 사용하도록 모두 등록되지만 JVM 옵션에 설정된 -Dspring.profiles.active 값에 따라 실제로 둘 중 하나만 ApplicationContext 인스턴스를 생성하는 데 사용됩니다.