ApplicationContext - Environment 1 - Profile

EULJIRO DEV·2021년 5월 6일

Application implements EnvironmentCapable

ApplicationContextBeanFactory의 역할 이외에도 다양한 기능을 제공한다.
(Environment, MessageSource, ApplicationEventPublisher, ResourceLoader)

Environment는 크게 2가지에 관련한 기능이 있는데 ProfileProperty이다.

Profile 설정을 통해, 활성화 할 빈과 그렇지 않은 빈을 구분할 수 있으며, test, stage, production 등 개발 환경에 구분을 두고 사용할 수 있다.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class AppRunner implements ApplicationRunner {
//1.
    @Autowired
    ApplicationContext ac;
//2.
    @Autowired
    Environment env;
//3.
    @Override
    public void run(ApplicationArguments args) throws Exception {
        Environment environment = ac.getEnvironment();
        System.out.println(Arrays.toString(environment.getActiveProfiles()));
        System.out.println(Arrays.toString(environment.getDefaultProfiles()));
        System.out.println(Arrays.toString(env.getActiveProfiles()));
        System.out.println(Arrays.toString(env.getDefaultProfiles()));

        System.out.println(ac);
        System.out.println(env);
    }
}

#1. ApplicationContext를 field injection 하였다.
#2. Environment로 주입을 받을 수 있다. (ApplcationContext에 등록되어있음)
#3. environment.getActiveProfiles() = 현재 활성화 된 profile 값들을 리턴하고
environment.getDefaultProfiles() = 디폴트로 설정 된 profile 값들을 리턴해준다.

package com.coretech.environment;


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.annotation.PostConstruct;
//-Dspring.profiles.active=stage
public class AppConfig {
//1.
    @Configuration
    //Profile에는 !, &, | 연산을 사용할 수 있습니다.
    @Profile({"local","dev"})
    public static class TestConfiguration{
        @PostConstruct
        void postC(){
            System.out.println("TEST!!!! OR LOCAL!!!");
        }
    }
//2.
    @Configuration
    @Profile("stage")
    public static class StageConfiguration{
        @PostConstruct
        void postC(){
            System.out.println("STAGE!!!!");
        }

    }
//.3
    @Configuration
    @Profile("product")
    public static class ProductConfiguration{
        @PostConstruct
        void postC(){
            System.out.println("PRODUCT!!!!");
        }
    }
}

#1 environment의 profile값이 local 혹은 dev일 때 @Configuration으로 ApplicationContext에 등록된다. @Configuration의 재미있는 점은, @Configuration으로 애노테이션 된 AppConfig자체가 ApplicationContext에 Bean으로 등록 되는 것이 아니라, Cglib 프록시 기술을 통해 AppConfig를 상속받은 Cglib 객체가 등록 된다는 것이다. 이 과정을 통해 @Configuration에 등록 된 빈들의 "Singleton" 프로토타입을 유지해줄 수 있으며, 자동으로 파라미터에 대응하는 Bean을 주입해 주는 등의 활약을 펼칠 수 있다.
#2. environment의 profile값이 stage 일 때 등록된다.
#3. environment의 profile값이 product일 때 등록된다.
add.

추가적으로 @Profile()의 인자 값에 &(and), !(not), |(or) 와 같은 연산 또한 함께 사용할 수 있다. 예를 들어 @Profile({"local","dev"}) 대신에, @Profile("local | dev") 로 표현할 수 있는 것이다.

VM options에 -Dspring.profiles.active="local"이라고 하면, @Profiles("local")인 @Bean들이 활성화 될것이다.

profile
https://github.com/hyunsooryu

0개의 댓글