1. 首先多环境的文件夹:
database.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://*.*.*.*:3306/*?useSSL=false
jdbc.username=***
jdbc.password=***
2. pom.xml配置:
<profiles>
<!-- 系统环境切换 dev为开发环境配置, test为测试环境配置,pro为生产环境配置 -->
<profile>
<id>test</id>
<properties>
<env>resource_test</env>
</properties>
<activation>
<!--默认不打这个包:false-->
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>pro</id>
<properties>
<env>resource_pro</env>
</properties>
<activation>
<!--默认不打这个包:false-->
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<build>
<!-- war包的名称,全局 -->
<finalName>procure</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
<!-- 指定需要编译的资源文件目录 -->
<resources>
<!-- 指定通用的资源文件目录 -->
<resource>
<directory>${basedir}/src/main/resources/</directory>
<!-- 指定通用的,编译的资源文件目录中所包含的文件类型 -->
<includes>
<include>*.*</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<!--设置自动替换-->
<filtering>true</filtering>
</resource>
<!-- 指定需要区分环境的,编译的资源文件目录中所包含的文件类型 -->
<resource>
<directory>${basedir}/src/main/resources/${env}</directory>
<includes>
<include>*.*</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3. 其次数据源配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:${env}/database.properties"/>
</bean>
<bean id="procureDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
......
4. 再次打包:(必须先clean,因为你替换xml文件了~~)
logback日志只修改日志路径即可;(具体logback日志参考另一篇博客)