<bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="張三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢與偏見</value>
<value>仲夏夜之夢</value>
<value>霧都孤兒</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
<value>排球</value>
</list>
</property>
<property name="cards">
<map>
<entry key="中國銀行" value="1545615345415"></entry>
<entry>
<key><value>農業銀行</value></key>
<value>54654861231543</value>
</entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>dota</value>
<value>cs</value>
<value>dnf</value>
<value>cf</value>
</set>
</property>
<property name="wife"><null/></property>
<property name="info">
<props>
<prop key="學號">2015534001</prop>
<prop key="sex">男</prop>
<prop key="name">張三</prop>
</props>
</property>
</bean>
- <profiles>
- <profile>
- <!-- 本地開發環境 -->
- <id>development</id>
- <properties>
- <profiles.active>development</profiles.active>
- <deploy.url>http://host:port/manager/text</deploy.url>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <!-- 測試環境 -->
- <id>test</id>
- <properties>
- <profiles.active>test</profiles.active>
- <deploy.url>http://host:port/manager/text</deploy.url>
- </properties>
- </profile>
- <profile>
- <!-- 生產環境 -->
- <id>production</id>
- <properties>
- <profiles.active>production</profiles.active>
- <deploy.url>http://host:port/manager/text</deploy.url>
- </properties>
- </profile>
- </profiles>
profile標籤的工作流程
1,一個環境配置一個profile標籤,在裏面配置環境信息
下面例子有本地開發環境,測試環境和正式環境共三個環境的配置信息,不同的配置裏面有不同的Redis服務器連接配置。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--<dependencies>等等配置,省掉 -->
<profiles>
<!-- 本地開發環境(Development) -->
<profile>
<id>Development</id>
<properties>
<!-- redis -->
<redis.host>192.168.14.73</redis.host>
<redis.port>6379</redis.port>
<redis.auth></redis.auth>
<redis.select></redis.select>
</properties>
<!-- 打包時不指定profile id,默認激活本環境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 測試環境(TEST) 提供給測試團隊的-->
<profile>
<id>Test</id>
<properties>
<!-- redis -->
<redis.host>192.168.14.200</redis.host>
<redis.port>6379</redis.port>
<redis.auth></redis.auth>
<redis.select></redis.select>
</properties>
</profile>
<!-- 正式生產環境(Production) -->
<profile>
<id>Production</id>
<properties>
<!-- redis -->
<redis.host>192.168.14.206</redis.host>
<redis.port>6379</redis.port>
<redis.auth>password</redis.auth>
<redis.select></redis.select>
</properties>
</profile>
</profiles>
- 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
- 37
- 38
- 39
- 40
- 41
- 42
- 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
- 37
- 38
- 39
- 40
- 41
- 42
2,java代碼中想要取得profile中的配置的話,要通過.properties文件來讀取
例redis.properties
#redis服務器
redis.host=${redis.host}
redis.port=${redis.port}
redis.auth=${redis.auth}
redis.select=${redis.select}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
3,.properties文件怎麼才能取到pom文件中profile定義的值呢,需要在pom文件中增加filtering標籤,加在build標籤內
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.class</exclude>
</excludes>
<filtering>true</filtering>
</testResource>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<!--<plugins>省掉 -->
</build>
- 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
- 37
- 38
- 39
- 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
- 37
- 38
- 39
4,java讀取.properties文件,使用ResourceBundle就可以了
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
//redis配置信息
String host = resourceBundle.getString("redis.host");//redis服務器host
String port = resourceBundle.getString("redis.port");//redis服務器端口
String auth = resourceBundle.getString("redis.auth");//redis服務器認證信息
String select = resourceBundle.getString("redis.select");//redis數據庫
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
5,打包的時候通過-P指定用哪一個profile的信息來編譯打包
`mvn package -PprofileId `