TODAY 3
Spring 개발환경 나누기

local : 개발자 각각의 PC
dev : 코드를 합처 테스트해볼 환경
Integration : 통합개발 환경 ( 컴포넌트로 나눠질시 사용함 )
qa : QA 엔지니어가 테스트 가능한 환경
staging : 운영과 거의 동일한 환경
production : 실제 서비스를 위한 환경



< 실제 설정 적용 >

< 1. Spring 소스 설정 >
Spring : 4.3.2
Java : 1.8
Tomcat : 8.5.24

소스의 구조는 이렇게 되어있습니다.

context-common.xml
<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xmlns="http://www.springframework.org/schema/beans";
        xmlns:http="http://www.springframework.org/schema/p";
        xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd";>
     <bean id="reloadingStrategy" class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"/>
 
    <beans profile="local">
         <bean id="configuration" class="org.apache.commons.configuration.XMLPropertiesConfiguration">
             <constructor-arg type="java.lang.String" value="classpath:config/properties/config-local.xml"/>
             <property name="reloadingStrategy" ref="reloadingStrategy"/>
         </bean>
     </beans>
 
    <beans profile="dev">
        <bean id="configuration" class="org.apache.commons.configuration.XMLPropertiesConfiguration">
            <constructor-arg type="java.lang.String" value="classpath:config/properties/config-dev.xml"/>
            <property name="reloadingStrategy" ref="reloadingStrategy"/>
        </bean>
    </beans>
 
    <beans profile="prod">
        <bean id="configuration" class="org.apache.commons.configuration.XMLPropertiesConfiguration">
            <constructor-arg type="java.lang.String" value="classpath:config/properties/config-prod.xml"/>
            <property name="reloadingStrategy" ref="reloadingStrategy"/>
        </bean>
    </beans>
 
</beans>
cs

context-datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xmlns="http://www.springframework.org/schema/beans";
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd";>
 
    <beans profile="local">
        <bean id="dataSourceSpied" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/chemilocalwas"/>
        </bean>
        <bean id="dataSource" class="net.sf.log4jdbc.sql.jdbcapi.DataSourceSpy">
            <constructor-arg ref="dataSourceSpied"/>
        </bean>
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
             <constructor-arg ref="sqlSessionFactory"/>
         </bean>
         <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:/config/db/mariaDB/mybatis-config.xml"></property>
         </bean>
         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
            <property name="basePackage" value="com.chemi.cheminoti.dao"></property>
         </bean>
    </beans>
 
    <beans profile="dev">
        <bean id="dataSourceSpied" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/chemidevwas"/>
        </bean>
        <bean id="dataSource" class="net.sf.log4jdbc.sql.jdbcapi.DataSourceSpy">
            <constructor-arg ref="dataSourceSpied"/>
        </bean>
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg ref="sqlSessionFactory"/>
        </bean>
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:/config/db/mariaDB/mybatis-config.xml"></property>
        </bean>
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
            <property name="basePackage" value="com.chemi.cheminoti.dao"></property>
        </bean>
    </beans>
 
    <beans profile="prod">
        <bean id="dataSourceSpied" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/chemiprodwas"/>
        </bean>
        <bean id="dataSource" class="net.sf.log4jdbc.sql.jdbcapi.DataSourceSpy">
            <constructor-arg ref="dataSourceSpied"/>
        </bean>
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg ref="sqlSessionFactory"/>
        </bean>
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:/config/db/mariaDB/mybatis-config.xml"></property>
        </bean>
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
            <property name="basePackage" value="com.chemi.cheminoti.dao"></property>
        </bean>
    </beans>
 
</beans>
cs


pom.xml 중간 부분
<profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <environment>local</environment>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <environment>dev</environment>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <environment>qa</environment>
            </properties>
        </profile>
        <profile>
            <id>stage</id>
            <properties>
                <environment>stage</environment>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <environment>prod</environment>
            </properties>
        </profile>
</profiles>
cs


< 2. 인텔리제이 + 로컬 톰캣 >
인텔리제이 톰캣에 해당 프로파일을 넣어줍니다.



Windwos 톰캣 설정파일 변경
${톰캣 설치 경로}\conf\context.xml


${톰캣 설치 경로}\conf\server.xml




< 3. 리눅스 서버 >


JVM 옵션 설정하기
${tomcat 설치 경로}/bin/catalina.sh

# cat catalina.sh | head -10

JAVA_OPTS="-DUSER_HOME=/${실제 설치한 경로}/apache-tomcat-8.5.24 -Dfile.encoding=\"utf-8\" -Djava.net.preferIPv4Stack=true -Dspring.profiles.active=dev -Djava.awt.headless=true -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxMetaspaceSize=1024m -XX:MetaspaceSize=2048m -XX:+DisableExplicitGC"

#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at


context.xml, server.xml 은 위 내용과 동일하게 설정


---> 정상적으로 세팅이 완료되면
---> Tomcat 실행시 프로파일 수정만으로 운영서버/개발서버/통합테스트 서버를 자동적으로 변경할수 있습니다.