Spring MVC中实现不同环境的配置

Spring MVC中实现不同环境的配置

 

在项目开发中,存在开发环境和生产环境,一些常用的配置,比如数据库,redis,properties文件中的一些变量等等,在开发环境和生产环境中,都会有一些差异,每次提交代码的时候,如果提交测试环境,则运维人员版本更新时总是要修改配置,危险性比较大,如果提交生产环境,则每次更新后,都要修改配置,才能本地启动,一些常用的思路都是提供多个配置文件,依据系统参数来加载不同的配置,或者是在本地打包好以后,直接上传包到生产服务器,Spring 3.1以后,提供了profile参数,无论是spring mvc项目,还是spring boot项目,都可以使用profile参数,来很方便的区分开发环境和生产环境。

 

在spring mvc中,可以通过<benas profile="">来加载不同的配置,不过一定要注意,该配置只能放在xml配置文件的最后,否则,项目启动会报异常。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
>


<!--
引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>

<property name="location" value="classpath:jdbc.properties"/>
</bean>

<!-- Mybatis session factory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
lazy-init="false"
>

<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/Configuration.xml"/>
<property name="mapperLocations" value="classpath*:mybatis/sqlmapper/*.xml"/>
</bean>
<!-- DAO
接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qiu.urongw.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (
事务管理)transaction manager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<beans profile="dev">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${test_url}"/>
<property name="username" value="${test_username}"/>
<property name="password" value="${test_password}"/>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
<property name="maxWait" value="${maxWait}"></property>
</bean>
</beans>

< beans profile="prod">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
<property name="maxWait" value="${maxWait}"></property>
</bean>
</beans>
</beans>

 

此处的beans profile="prod"必须要在所有配置的最后

 

Profile需要在启动的时候,指定当前的环境,如果不指定,默认采用spring.profiles.default指定的环境,可以在web.xml中,加上

<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>prod</param-value>
</context-param>

 

指定默认的环境是prod,这样,运维人员在生产环境版本更新的时候,就不需要做任何额外的操作,本地测试的时候,可以添加系统变量,也可以在jvm参数处添加-Dspring.profiles.active=dev指定启动环境是dev,如果本地测试的时候,不添加参数,则由于本地一般无法和生产环境连通,项目启动的时候,一般会出现异常,所以不用担心测试时连接到生产环境。

 

可以在代码中根据指定的参数,加载不同的properties配置文件

String profile = System.getProperty("spring.profiles.active");
if(null == profile){
}
if(null != profile && profile.trim().toLowerCase().equals("dev")){
}

猜你喜欢

转载自blog.csdn.net/G1248019684/article/details/79579463