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>
  
<!-- 非静态方法,使用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>
   
<!-- 静态方法,无需使用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 阅读(323) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: