posts - 495,comments - 227,trackbacks - 0

          有兩種用法,一種是直接吧方法的返回值注入到實例的屬性,二是用返回值定義成Bean

          定義實體:

          package Bean.superIOCmethod;

          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.superIOCmethod;

          public class Son {
            
          private String age;

          public String getAge() {
              
          return age;
          }


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

          }
          package Bean.superIOCmethod;
          public class Field {
             public static final String TEST_FIELD="welcom";
             public String getValue(){
              return "test";
             }
             public static String getStaticValue(){
              return "static test";
             }
          }


          配置文件:

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

          <beans>
            
          <!-- 非靜態(tài)方法,使用targetObject -->
            
          <bean id="son" class="Bean.superIOCmethod.Son">
               
          <property name="age">
                 
          <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                    
          <property name="targetObject">
                        
          <ref local="value"/>
                    
          </property>
                    
          <property name="targetMethod">
                       
          <value>getValue</value>
                    
          </property>
                 
          </bean>
               
          </property>
            
          </bean>
             
          <!-- 靜態(tài)方法,無需使用targetObject,但要配置targetClass -->
            
          <bean id="staticson" class="Bean.superIOCmethod.Son">
               
          <property name="age">
                 
          <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                    
          <property name="targetClass">
                      
          <value>Bean.superIOCmethod.Field</value>
                    
          </property>
                    
          <property name="targetMethod">
                       
          <value>getStaticValue</value>
                    
          </property>
                 
          </bean>
               
          </property>
            
          </bean>
            
            
          <!-- 將方法返回值定義成Bean -->
            
          <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                 
          <property name="targetClass">
                   
          <value>java.lang.System</value>
                 
          </property>
                 
          <property name="targetMethod">
                   
          <value>getProperties</value>
                 
          </property>
             
          </bean>
             
             
          <bean id="javaversion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                
          <property name="targetObject">
                  
          <ref local="sysProps"/>
                
          </property>
                
          <property name="targetMethod">
                  
          <value>getProperty</value>
                
          </property>
                
          <property name="arguments">
                   
          <list>
                     
          <value>java.version</value>
                   
          </list>
                
          </property>
             
          </bean>
            
          <bean id="value" class="Bean.superIOCmethod.Field">
            
          </bean>
          </beans>

          posted on 2011-10-17 11:30 SIMONE 閱讀(328) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 安乡县| 神木县| 湘阴县| 罗江县| 荣成市| 乳山市| 宜宾县| 邢台市| 元阳县| 延津县| 喀什市| 寿光市| 香港| 齐河县| 施秉县| 阜阳市| 永定县| 乳山市| 台山市| 长沙县| 扶绥县| 荔波县| 万州区| 中方县| 陇川县| 绥江县| 嘉鱼县| 拉萨市| 庆城县| 黄石市| 七台河市| 苏尼特左旗| 商河县| 呼伦贝尔市| 宕昌县| 太湖县| 平乐县| 德令哈市| 东山县| 禄丰县| 图木舒克市|