Spring profiles使用
新建一个profiles.xml文件
1
2
3
4
5
6
7
8
9
10
11<beans profile="dev">
<context:property-placeholder location="classpath:dev/jdbc.properties,classpath:dev/SEVS.properties" />
</beans>
<beans profile="test">
<context:property-placeholder location="classpath:test/jdbc.properties,classpath:test/SEVS.properties" />
</beans>
<beans profile="prod">
<context:property-placeholder location="classpath:prod/jdbc.properties,classpath:prod/SEVS.properties" />
</beans>把profiles.xml 导入到applicationContext.xml中
1
2
3
4
5
6
7<!--数据库jdbc配置文件-->
<import resource="classpath:profiles.xml" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />在web.xml中激活
1
2
3
4<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
Maven profile使用
在pom.xml中添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36<project>
...
...
<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.xml</include>
<include>log4j.properties</include>
<include>${profiles.active}/*</include>
</includes>
</resource>
</resources>
</build>
...
...
</project>当前类路径的目录结构是
src/main/resources/- dev
- jdbc.properties
- SEVS.properties
- test
- jdbc.properties
- SEVS.properties
- prod
- jdbc.properties
- SEVS.properties
- log4j.properties
- applicationContext.xml
- dispatcher-servlet.xml
- dev
执行命令
mvn clean package -DskipTests -Pdev
-DskipTests 跳过单元测
-Pdev 选择激活dev环境(第一步中默认激活了dev环境)在生成的war包中,查看classes目录下只有dev子目录,没有test prod等无关的目录。
这种方法需要手动更改web.xml中的spring.profiles.active
Spring和Maven Profiles的区别
Spring Profiles 是在运行起作用
Maven Profiles 是在打包时起作用,为了排除不必要的环境配置文件
两者用途不一样。