신규 블로그를 만들었습니다!

2020년 이후부터는 아래 블로그에서 활동합니다.

댓글로 질문 주셔도 확인하기 어려울 수 있습니다.

>> https://bluemiv.tistory.com/

상황에 따라 개발환경 바꾸기

동일한 환경의 xml파일을 여러개 만든다.

예를들어, 개발환경과 실환경

applicationCTX_dev.xml

applicationCTX_run.xml

 

위 2개의 환경을 내가 원하는 상황에 맞게 실행해보자

 

필요한 파일 (총 5개)

applicationCTX_dev.xml (개발환경 Context)

applicationCTX_run.xml (실환경 Context)

server.properties (프로퍼티 파일)

Main.java (메인)
ServerInfo.java(단순한 VO)
 

applicationCTX_dev.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    profile="dev">
    
    <context:property-placeholder location="classpath:server.properties"/>
    
    <bean id="serverInfo" class="com.edu.exam01.ServerInfo">
        <property name="ip"><value>${server.dev.ip}</value></property>
        <property name="port"><value>${server.dev.port}</value></property>
    </bean>
 
</beans>​

 

applicationCTX_run.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    profile="run">
    
    <context:property-placeholder location="classpath:server.properties"/>
    
    <bean id="serverInfo" class="com.edu.exam01.ServerInfo">
        <property name="ip"><value>${server.ip}</value></property>
        <property name="port"><value>${server.port}</value></property>
    </bean>
 
 
</beans>​

여기서 중요한 부분이 있는데,

xml 파일의 namespace부분에 profile 속성이다.

profile속성값을 dev와 run으로 설정해서

xml파일을 구분한다.

 

그래서 꼭 넣어줘야하는 부분이다.

 

server.properties

server.dev.ip=localhost
server.dev.port=8888
server.ip=213.186.229.29
server.port=80​

개발환경일때는 localhost, 8888로 설정,

실제환경일떄는 213.186.229.29, 80으로 설정했다.

 

ServerInfo.java

package com.edu.exam01;
 
public class ServerInfo {
 
    private String ip;
    private int port;
 
    // constructor
    public ServerInfo(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }
 
    public ServerInfo() {
 
    }
 
    // get set method
    public String getIp() {
        return ip;
    }
 
    public void setIp(String ip) {
        this.ip = ip;
    }
 
    public int getPort() {
        return port;
    }
 
    public void setPort(int port) {
        this.port = port;
    }
 
    @Override
    public String toString() {
        return "ServerInfo [ip=" + ip + ", port=" + port + "]";
    }
 
}​

단순한 VO 파일

 

Main.java

package com.edu.exam01;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class Main {
 
    public static void main(String[] args) {
 
        /*
         * 1. 값 입력 받기
         */
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = ""; // 입력할 값의 변수
        try {
            input = br.readLine();
        } catch (Exception e) {
            // TODO: handle exception
        }
        
        String config = "";
        if (input.equalsIgnoreCase("dev")) {
            // 개발 환경
            config = "dev";
        } else if (input.equalsIgnoreCase("run")) {
            // 실환경
            config = "run";
        } else {
            // 잘못된 입력 - 시스템 종료
            System.exit(0);
        }
        
        /*
         * 2. 입력받은 값으로, 알맞게 실행
         */
        GenericXmlApplicationContext gCtx = new GenericXmlApplicationContext();
        gCtx.getEnvironment().setActiveProfiles(config);// 위에서 입력했던 값으로 실행
        gCtx.load("classpath:applicationCTX_dev.xml", "classpath:applicationCTX_run.xml");
        gCtx.refresh();
        
        // 출력해보기
        ServerInfo serverInfo = gCtx.getBean("serverInfo", ServerInfo.class);
        System.out.println(serverInfo.toString());
 
        gCtx.close();
    }
}​

 

if문을 이용해서

개발환경을 원할때는 dev라고 입력받고

실환경을 원할때는 run이라고 입력받는다.

 

그리고, context 파일을 가져온다.

이때, setActiveProfiles() 메소드를 이용해서,

방금 입력받은 값으로 (즉, 원하는 환경으로) 실행한다.

 

gCtx.getEnvironment().setActiveProfiles(config);

 

그리고, 원하는 환경으로 실행이 됐는지 확인하기 위해 출력해본다.

 

 

결과

1. dev 로 실행하기

 

2. run으로 실행하기

 

 

 

※ 이 글은 Seoul Wiz - '실전 Spring 강좌'를 요약하여 작성하였습니다.

>> 유튜브 - SeoulWiz

 

[프리렉]서블릿/JSP웹 프로그래밍 with HTML+CSS+XML+자바스크립트

 

www.youtube.com

 

 

 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기