posts - 495,comments - 227,trackbacks - 0

          實(shí)際應(yīng)用中,某個(gè)實(shí)例的屬性可能是另一個(gè)對(duì)象的一個(gè)屬性,Spring支持將bean實(shí)例的屬性值直接賦值給一個(gè)變量

          屬性值的注入,是通過PropertyPathFactoryBean完成的,PropertyPathFactoryBean用來獲取目標(biāo)bean的屬性,獲得的值可以注入到其他bean,也可以定義成新的bean

          實(shí)體類:

          package Bean.superIOCparam;

          public class Person {
             
          private Son son;
             
          private String age;
          public String getAge() {
              
          return age;
          }

          public void setAge(String age) {
              
          this.age = age;
          }

          public Son getSon() {
              
          return son;
          }

          public void setSon(Son son) {
              
          this.son = son;
          }

          }



          package Bean.superIOCparam;

          public class Son {
            
          private String age;

          public String getAge() {
              
          return age;
          }


          public void setAge(String age) {
              
          this.age = age;
          }

          }

           配置文件:提供四種注入

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

            
          <bean id="person" class="Bean.superIOCparam.Person" singleton="false">
               
          <property name="age">
                  
          <value>30</value>
               
          </property>
               
          <property name="son">
                  
          <bean class="Bean.superIOCparam.Son">
                     
          <property name="age">
                        
          <value>16</value>
                     
          </property>
                  
          </bean>
               
          </property>
            
          </bean>
            
            
          <!--如下將會(huì)將person的屬性son的屬性age傳入son1實(shí)例的age屬性-->
              
          <bean id="son1" class="Bean.superIOCparam.Son">
                  
          <property name="age">
                    <!--以下是訪問bean屬性的簡單方式,這樣可以將person這個(gè)bean的age屬性賦值給son1這個(gè)bean的age屬性-->
                     
                   <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
                  
          </property>
              
          </bean>
              
              
          <!-- 以下將會(huì)獲得結(jié)果son,它將是person bean的son的數(shù)值-->
              
          <bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
                 
          <property name="targetBeanName">
                   
          <value>person</value>
                 
          </property>
                 
          <property name="propertyPath">
                   
          <value>son</value>
                 
          </property>
              
          </bean>
              
               
          <!-- 以下將會(huì)獲得結(jié)果16,它將是person bean的son的age屬性-->
              
          <bean id="son3" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
                 
          <property name="targetBeanName">
                   
          <value>person</value>
                 
          </property>
                 
          <property name="propertyPath">
                   
          <value>son.age</value>
                 
          </property>
              
          </bean>
              
              
          <!-- 以下會(huì)獲得結(jié)果為30 ,它將是獲得該bean的內(nèi)部bean的age屬性-->
              
          <bean id="son4" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
                  
          <property name="targetObject">
                      
          <bean class="Bean.superIOCparam.Person">
                          
          <property name="age"><value>30</value></property>
                      
          </bean>
                  
          </property>
                  
          <property name="propertyPath"><value>age</value></property>
              
          </bean>
          </beans>


          測試代碼:

          public static void main(String[] args) throws Exception {
                  
                  String path
          =new Test().getClass().getResource("/").getPath();
                  String realpath
          =path.substring(1, path.length());
                  ApplicationContext context
          =new FileSystemXmlApplicationContext(realpath+"/superIOCparam.xml");
                  Son son1
          =(Son)context.getBean("son1");
                  Son son2
          =(Son)context.getBean("son2");

                  System.out.println(
          "person age is:"+son1.getAge());
                  System.out.println(
          "person age is:"+son2.getAge());
                  System.out.println(context.getBean(
          "son3"));
                  System.out.println(context.getBean(
          "son4"));
              }


          運(yùn)行結(jié)果:

          person age is:16
          person age is:16
          16
          30

          posted on 2010-08-10 17:04 SIMONE 閱讀(1005) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 清新县| 沾化县| 克什克腾旗| 渝中区| 汉沽区| 宁河县| 松滋市| 潍坊市| 商丘市| 阜新| 乐亭县| 新郑市| 丹江口市| 来宾市| 阜平县| 手机| 松原市| 尚志市| 和顺县| 阿勒泰市| 房山区| 墨玉县| 乐都县| 英山县| 青浦区| 陕西省| 长岛县| 永清县| 马龙县| 黑山县| 左贡县| 湖口县| 津南区| 海阳市| 仲巴县| 嘉荫县| 马鞍山市| 定日县| 彰化市| 天津市| 兰溪市|