프로파일(profile) 속성을 이용한 설정 - XML 설정 파일을 이용하는 방법, JAVA 설정 파일을 이용하는 방법

CODEDRAGON Development/Spring

반응형

 

 

프로파일(profile) 속성을 이용한 설정

·         동일한 스프링 빈을 여러 개 만들어 놓고 상황(환경)에 따라서 적절한 스프링 빈을 사용할 수 있습니다. profile 속성을 사용하면 됩니다.

·         개발환경에 대한 설정과 운영환경에 대한 설정을 분리할 수 있습니다.

·         운영체제에 정보를 확인한 후 윈도우면 윈도우 설정을 리눅스이면 리눅스에 맞는 설정을 구분해서 자동으로 적용할 수 있습니다.

 

 

 

프로파일(profile) 속성을 이용한 설정 방법 종류

·         XML 설정 파일을 이용하는 방법

·         JAVA 설정 파일을 이용하는 방법

 

 

 

XML 설정 파일을 이용하는 방법

 


 

 

MainClass.java

config = "dev";

 

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();

// 활성화 시킬 프로파일명을 설정합니다.

ctx.getEnvironment().setActiveProfiles(config);

ctx.load("appCTX_dev.xml", "appCTX_run.xml");

 

 

appCTX_dev.xml

beans 요소에 profile속성을 추가해서  XML설정 파일을 식별합니다.

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="…"

profile="dev">

 

appCTX_run.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="…"

profile="run">

 

 

 

JAVA 설정 파일을 이용하는 방법

 


 

 

MainClass.java

config = "dev";

 

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

ctx.getEnvironment().setActiveProfiles(config);

ctx.register(ApplicationConfigDev.class, ApplicationConfigRun.class);

 

 

ApplicationConfigDev.java

어노테이션으로 프로파일을 설정합니다.

@Configuration

@Profile("dev")

public class ApplicationConfigDev {

@Bean

public ServerInfo serverInfo() {

//

}

 

}

 

 

ApplicationConfigRun.java

@Configuration

@Profile("run")

public class ApplicationConfigRun {

@Bean

public ServerInfo serverInfo() {

//

}

 

}

 

 

반응형