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 閱讀(999) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 延川县| 荥经县| 霍城县| 巴林右旗| 东乌珠穆沁旗| 宜春市| 余姚市| 景洪市| 舟山市| 呼伦贝尔市| 涟源市| 道孚县| 台山市| 桂阳县| 洪雅县| 黔江区| 金川县| 富裕县| 南投县| 堆龙德庆县| 疏勒县| 龙陵县| 大邑县| 钦州市| 沁水县| 德江县| 将乐县| 临夏市| 南昌市| 克拉玛依市| 南皮县| 邹城市| 左权县| 济宁市| 自贡市| 威远县| 贵州省| 阿坝县| 惠州市| 新泰市| 社旗县|