ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • IoC 컨테이너와 빈 (3) - Enviornment - 프로파일
    백기선(인프런 강의)/스프링 프레임워크 핵심 기술 2020. 3. 17. 13:10
    반응형

    EnvironmentCapable - 프로파일

    ApplicationContext 가 아래와같은 인터페이스를 상속 받고 있다.

    public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
    		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
    
    	/**
    	 * Return the unique id of this application context.
    	 * @return the unique id of the context, or {@code null} if none
    	 */
    	String getId();
    
    	/**
    	 * Return a name for the deployed application that this context belongs to.
    	 * @return a name for the deployed application, or the empty String by default
    	 */
    	String getApplicationName();
    
    	/**
    	 * Return a friendly name for this context.
    	 * @return a display name for this context (never {@code null})
    	 */
    	String getDisplayName();
    
    	/**
    	 * Return the timestamp when this context was first loaded.
    	 * @return the timestamp (ms) when this context was first loaded
    	 */
    	long getStartupDate();
    
    	/**
    	 * Return the parent context, or {@code null} if there is no parent
    	 * and this is the root of the context hierarchy.
    	 * @return the parent context, or {@code null} if there is no parent
    	 */
    	ApplicationContext getParent();
    
    	/**
    	 * Expose AutowireCapableBeanFactory functionality for this context.
    	 * <p>This is not typically used by application code, except for the purpose of
    	 * initializing bean instances that live outside of the application context,
    	 * applying the Spring bean lifecycle (fully or partly) to them.
    	 * <p>Alternatively, the internal BeanFactory exposed by the
    	 * {@link ConfigurableApplicationContext} interface offers access to the
    	 * {@link AutowireCapableBeanFactory} interface too. The present method mainly
    	 * serves as a convenient, specific facility on the ApplicationContext interface.
    	 * <p><b>NOTE: As of 4.2, this method will consistently throw IllegalStateException
    	 * after the application context has been closed.</b> In current Spring Framework
    	 * versions, only refreshable application contexts behave that way; as of 4.2,
    	 * all application context implementations will be required to comply.
    	 * @return the AutowireCapableBeanFactory for this context
    	 * @throws IllegalStateException if the context does not support the
    	 * {@link AutowireCapableBeanFactory} interface, or does not hold an
    	 * autowire-capable bean factory yet (e.g. if {@code refresh()} has
    	 * never been called), or if the context has been closed already
    	 * @see ConfigurableApplicationContext#refresh()
    	 * @see ConfigurableApplicationContext#getBeanFactory()
    	 */
    	AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
    
    }
    @Controller
    public class HomeController {
    	@Autowired
    	ApplicationContext ctx;
    	@RequestMapping(value = "/", method = RequestMethod.GET)
    	public String home(Locale locale, Model model) {
    		// ApplicationContext가 EnvironmentCapable을 상속받고
    		// EnvironmentCapable가 Environment을 상속 받았기 때문에 아래와같이 사용가능
    		Environment environment = ctx.getEnvironment();
    		System.out.println("프로퍼티 : " + Arrays.toString(environment.getActiveProfiles()));
    		System.out.println("defalt 프로퍼티 : " + Arrays.toString(environment.getDefaultProfiles()));
    		
    		return "home";
    	}
    }

    프로파일 실행 결과

     

    프로파일이란?

    • 기능을 충족시키기 위해 프로파일이란는 기능이 추가 됨
      • 개발 환경에 따른 빈들의 묶음
        • ex) 어떤 환경에서는 이 빈들을 사용하겠다.  
      • 특정 환경에서만 빈을 등록하는 경우
    • Environment의 역할은 활성화할 프로파일 확인 및 설정

    프로파일 설정

    방법1 - 프로젝트 우클릭 > Run as > Run configurations

     

     

    방법2 - web xml 수정

    <context-param>
      <param-name>springprofiles.active<param-name>
      <param-value>test</param-value>
    </context-param>

     

    프로파일 정의

    방법1 - 클래스에 정의

    • @Configuration @Profile("test")
    • @Coponent @Profile("test")
    @Repository
    @Scope("test")
    public class TestbookRepository implements BookRepository{
    }

    방법2 - 메소드에 정의

    @Configuration
    public class TestConfiguration {
    	@Bean
    	@Profile("test")
    	public BookRepository bookrepository() {
    		return new TestbookRepository();
    	}
    }

     

    프로파일을 정의할 때 Profile("!test") 프로파일이름 앞에다가 ! 를 붙이게되면 해당 프로파일 이름을 참조안할 때 빈을 만들겠다는 것이다.

    반응형
Designed by Tistory.