spring 注入例子


<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>



  1. <profiles>  
  2.     <profile>  
  3.         <!-- 本地开发环境 -->  
  4.         <id>development</id>  
  5.         <properties>  
  6.             <profiles.active>development</profiles.active>  
  7.             <deploy.url>http://host:port/manager/text</deploy.url>  
  8.         </properties>  
  9.         <activation>  
  10.             <activeByDefault>true</activeByDefault>  
  11.         </activation>  
  12.     </profile>  
  13.     <profile>  
  14.         <!-- 测试环境 -->  
  15.         <id>test</id>  
  16.         <properties>  
  17.             <profiles.active>test</profiles.active>  
  18.             <deploy.url>http://host:port/manager/text</deploy.url>  
  19.         </properties>  
  20.     </profile>  
  21.     <profile>  
  22.         <!-- 生产环境 -->  
  23.         <id>production</id>  
  24.         <properties>  
  25.             <profiles.active>production</profiles.active>  
  26.             <deploy.url>http://host:port/manager/text</deploy.url>  
  27.         </properties>  
  28.     </profile>  
  29. </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 `


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值