針對生產環境,測試環境,以及本地調試開發有時會配置多套數據庫,在一個數據配置文件進行修改,往往有時發布到生成環境會忘記修改,或者本地調試時還是生產環境的庫,會導致生產環境數據被污染。
ps--剛開始配完發現在Myeclipse一直是“development”模式,后來發現tomcat配置完之后要myeclise中進行jdk配置。
1.這里我們可以配置多個數據源配置文件:

application.development.properties 作為開發環境;
application.local.properties 作為本地調試環境;
application.properties 作為生產環境;
application.test.properties 作為測試環境;
jdbc.driver=com.mysql.jdbc.Driver #development jdbc.url=jdbc:mysql://ip:port/database?autoReconnect=true&initialTimeout=3&useUnicode=true&characterEncoding=utf-8 jdbc.username=user jdbc.password=password #connection pool settings jdbc.pool.minIdle=1 jdbc.pool.maxIdle=3 jdbc.pool.maxActive=30 jdbc.pool.maxWait=12000
2.然后在applicationContext.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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
default-lazy-init="true">
<description>Spring公共配置 </description>
<context:annotation-config />
<!-- 使用annotation 自動注冊bean, 並保證@Required、@Autowired的屬性被注入 -->
<context:component-scan base-package="com.eteclab.wodm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- Jpa Entity Manager 配置 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="packagesToScan" value="com.eteclab"/>
<property name="jpaProperties">
<props>
<!-- 命名規則 My_NAME->MyName -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
</bean>
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform">
<bean factory-method="getDialect" class="org.eteclab.modules.persistence.Hibernates">
<constructor-arg ref="dataSource"/>
</bean>
</property>
</bean>
<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.eteclab.wodm" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<!-- Jpa 事務配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 使用annotation定義事務 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- JSR303 Validator定義 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<!-- production環境 -->
<beans profile="production">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:/application.properties"/>
<!-- 數據源配置, 使用Tomcat JDBC連接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 連接數控制與連接歸還策略 -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="${jdbc.pool.minIdle}" />
<property name="maxWait" value="${jdbc.pool.maxWait}" />
<property name="defaultAutoCommit" value="false" />
<!-- 連接Idle一個小時后超時 -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<!-- 應對網絡不穩定的策略 -->
<!-- <property name="testOnBorrow" value="true" />
<property name="validationInterval" value="30000" />
<property name="validationQuery" value="select 1 from dual" /> -->
<property name="testOnReturn" value="true"></property>
<property name="testWhileIdle" value="true"></property>
<property name="testOnBorrow" value="true" />
<property name="validationInterval" value="30000" />
<property name="validationQuery" value="select 1 from dual" />
<!-- 應對連接泄漏的策略 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
<!-- 數據源配置,使用應用服務器的數據庫連接池 -->
<!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB" />-->
</beans>
<!-- local development環境 -->
<beans profile="development">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.development.properties" />
<!-- Tomcat JDBC連接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 連接數控制與連接歸還策略 -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="${jdbc.pool.minIdle}" />
<property name="maxWait" value="${jdbc.pool.maxWait}" />
<property name="defaultAutoCommit" value="false" />
<!-- 連接Idle一個小時后超時 -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<!-- 應對網絡不穩定的策略 -->
<property name="testOnBorrow" value="true" />
<property name="validationInterval" value="30000" />
<property name="validationQuery" value="select 1 from dual" />
<!-- 應對連接泄漏的策略 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
</beans>
<!-- local test 環境 -->
<beans profile="local">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.local.properties" />
<!-- Tomcat JDBC連接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 連接數控制與連接歸還策略 -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="${jdbc.pool.minIdle}" />
<property name="maxWait" value="${jdbc.pool.maxWait}" />
<property name="defaultAutoCommit" value="false" />
<!-- 連接Idle一個小時后超時 -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<!-- 應對網絡不穩定的策略 -->
<property name="testOnBorrow" value="true" />
<property name="validationInterval" value="30000" />
<property name="validationQuery" value="select 1 from dual" />
<!-- 應對連接泄漏的策略 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
</beans>
<!-- unit test環境 -->
<beans profile="test">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.test.properties" />
<!-- Tomcat JDBC連接池 -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 連接數控制與連接歸還策略 -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="${jdbc.pool.minIdle}" />
<property name="maxWait" value="${jdbc.pool.maxWait}" />
<property name="defaultAutoCommit" value="false" />
<!-- 連接Idle一個小時后超時 -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<!-- 應對網絡不穩定的策略 -->
<property name="testOnBorrow" value="true" />
<property name="validationInterval" value="30000" />
<property name="validationQuery" value="select 1 from dual" />
<!-- 應對連接泄漏的策略 -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
</beans>
</beans>
3.對tomcat服務器進行修改:
{tomcat_home}
/bin/catalina.bat 或 catalina.sh 以確定tomcat所在服務器的環境
{production, development, local, test}
對於windows操作系統,在catalina.bat的第二行,增加如下的語句
set CATALINA_OPTS=%CATALINA_OPTS% -Dspring.profiles.active="production"
對於linux操作系統,在catalina.sh的第二行,增加如下的語句
CATALINA_OPTS="$CATALINA_OPTS -Dspring.profiles.active=\"production\""
注意這里的"production",只能是{production, development, local, test}中的一個
例如我在我本地開發,使用“local”配置:

還有一步要注意的地方就是在web.xml文件中:

配置默認為開發環境,這樣如果新接觸項目的開發人員如果本地沒有配置tomcat,也不會觸及到生產環境。
*************************************************************************************************
*************************************************************************************************
這里我們可以在項目中寫一個監聽類,來監聽項目運行時所屬的環境:
public class InitConfigListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
//偵測jvm環境,並緩存到全局變量中
String env = System.getProperty("spring.profiles.active");
if(env == null) {
Config.ENV = "development";
} else {
Config.ENV = env;
}
System.out.println("==================================================================================================");
System.out.println("The Application "+sce.getServletContext().getServletContextName()+" is running on the environment:" + Config.ENV);
System.out.println("==================================================================================================");
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
public class Config {
public static String ENV = "development";//默認開發常量
}
直接啟動tomcat看到如下效果:

當然我們更希望是在Myeclise開發工具中啟動- -

最后啟動tomcat就出來了= =

