posts - 495,comments - 227,trackbacks - 0

          實際應用中,某個實例的屬性可能是另一個對象的一個屬性,Spring支持將bean實例的屬性值直接賦值給一個變量

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

          實體類:

          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>
            
            
          <!--如下將會將person的屬性son的屬性age傳入son1實例的age屬性-->
              
          <bean id="son1" class="Bean.superIOCparam.Son">
                  
          <property name="age">
                    <!--以下是訪問bean屬性的簡單方式,這樣可以將person這個bean的age屬性賦值給son1這個bean的age屬性-->
                     
                   <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
                  
          </property>
              
          </bean>
              
              
          <!-- 以下將會獲得結果son,它將是person bean的son的數值-->
              
          <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>
              
               
          <!-- 以下將會獲得結果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>
              
              
          <!-- 以下會獲得結果為30 ,它將是獲得該bean的內部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"));
              }


          運行結果:

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

          posted on 2010-08-10 17:04 SIMONE 閱讀(1000) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 平江县| 衡南县| 双桥区| 靖远县| 体育| 方正县| 东乌珠穆沁旗| 黄石市| 镇坪县| 丘北县| 九寨沟县| 浦东新区| 平昌县| 嘉祥县| 石城县| 苏尼特左旗| 进贤县| 五寨县| 门源| 河北区| 乌鲁木齐市| 巴里| 依兰县| 苗栗县| 信阳市| 涞源县| 德化县| 平阳县| 新乐市| 桂平市| 兴国县| 福贡县| 东海县| 镇远县| 土默特右旗| 科技| 澎湖县| 商都县| 景德镇市| 临高县| 逊克县|