使用Spring3.1后的的Profile配置使不同环境加载不同配置文件

需求:项目运行在不同的环境中,每次运行项目的时候,都要重新手动配置配置文件,这样很容易出错,还好Spring3.1以后又出良心之作–profile,看了其他大佬一些做法,感触颇深,但仍然有疑惑,在这里提出来跟大家探讨一下。

配置文件结构:

|---config
    |---common
        |---common.properties
    |---dev
        |---config-dev.properties
        |---dpconfig-dev.properties
    |---production
        |---config-production.properties
        |---dpconfig-production.properties
    |---spring
        |---applicationContext-profile.xml
        |---applicationContext-spring.xml
    |---test 
        |---config-test.properties
        |---dpconfig-test.properties

applicationContext-profile.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:util="http://www.springframework.org/schema/util"
       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.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 测试环境配置文件 -->
    <beans profile="test">
        <context:property-placeholder location="classpath:test/*.properties, classpath:common/*.properties" />
    </beans>

    <!-- 生产环境配置文件 -->
    <beans profile="production">
        <context:property-placeholder location="classpath:production/*.properties, classpath:common/*.properties" />
    </beans>

    <!-- 开发环境配置文件 -->
    <beans profile="development">
        <context:property-placeholder location="classpath:dev/*.properties, classpath:common/*.properties" />
    </beans>

</beans>

applicationContext-spring.xml

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name="url" value="${url}"/>
        <!--<property name="username" value="${username}"/>
        <property name="password" value="${password}"/>-->
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="filters" value="${filters}"/>
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="${maxActive}"/>
        <!-- 初始化连接数量 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${maxWait}"/>
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${minIdle}"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>
        <property name="validationQuery" value="${validationQuery}"/>
        <property name="testWhileIdle" value="${testWhileIdle}"/>
        <property name="testOnBorrow" value="${testOnBorrow}"/>
        <property name="testOnReturn" value="${testOnReturn}"/>
        <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}"/>
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="${removeAbandoned}"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="${logAbandoned}"/>
    </bean>
    <import resource="applicationContext-profile.xml"/>

事务方面省略,在web.xml中别忘了初始化spring容器.
web.xml

 <!-- 配置spring的默认profile
         可选值:production(生产环境) test(测试环境) development(开发环境) -->
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>test</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

对于激活Profile的四种方式:
第二种和第三种都试过,其他两种有机会再测。

1.ENV方式: ConfigurableEnvironment.setActiveProfiles("test")
2.JVM参数方式: -Dspring.profiles.active="test"
3.web.xml方式:
<init-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>test</param-value>
</init-param>
4.标注方式(junit单元测试非常实用):
@ActiveProfiles({"test","productprofile"})

可能是我的理解力不行,所以在这我把我的理解和做法详细的说明一下(仅限第二种和第三种):
一、对于JVM参数方式:
①如果在linux系统中运行(项目已经打成war包发布到tomcat),需要修改bin/catalina.sh, vim 打开文件,添加:-Dspring.profiles.active="test"后再运行tomcat即可。
②如果在windows系统中运行(项目已经打成war包发布到tomcat),需要修改bin/catalina.bat 添加set JAVA_OPTS="-Dspring.profiles.active=test"即可
③如果是用Eclipse运行项目,需要右键项目run as – run configuration – Arguments – VM arguments加入-Dspring.profiles.active="test" 运行项目即可
④如果是用IDEA运行项目,需要点击上方工具栏Run – Edit Configurations,项目第一次运行的话需要配置:点击左上角+号,选Tomcat Server – Local,在Application server配置本地的tomcat,在JRE处配置JDK,在VM options中添加-Dspring.profiles.active="test"运行项目即可
二、对于web.xml方式:
在各个环境中修改

<context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>test</param-value>
    </context-param>

的值即可。
此处,发现一个问题:之前的配置文件为:

url:jdbc:mysql://***.**.**.***/numysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&allowMultiQueries=true
driverClassName:com.mysql.cj.jdbc.Driver
username:numysql
password:123456

这样的话,一直连接不上数据库一直报异常:
java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
后经试验,应该是编码问题,改成这种mysql的用户名和密码拼接在url后即可解决问题:

url:jdbc:mysql://***.**.**.***/numysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&allowMultiQueries=true&user=numysql&password=123456
driverClassName:com.mysql.cj.jdbc.Driver

猜你喜欢

转载自blog.csdn.net/peaceForEveryOne/article/details/78125175