Spring MethodInvokingFactoryBean注入Method值

14 篇文章 0 订阅

原文地址:http://blog.csdn.net/daryl715/article/details/1543630


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>

测试代码

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+"/superIOCmethod.xml");
        Son son1=(Son)context.getBean("son");
        Son son2=(Son)context.getBean("staticson");
        
        
        System.out.println("person age is:"+son1.getAge());
        System.out.println("person age is:"+son2.getAge());
        System.out.println(context.getBean("sysProps"));
        System.out.println(context.getBean("javaversion"));
 }

结果:



person age is:test
person age is:static test


{java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=D:/Program Files/MyEclipse Enterprise Workbench 5.1.0 GA/jre/bin, java.vm.version=1.5.0_08-b03, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=CN, sun.os.patch.level=Service Pack 2, java.vm.specification.name=Java Virtual Machine Specification, user.dir=E:/my/TestSpring, java.runtime.version=1.5.0_08-b03, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=D:/Program Files/MyEclipse Enterprise Workbench 5.1.0 GA/jre/
.
.
.
.
com.genuitec.eclipse.j2eedt.core_5.1.0/data/libraryset/1.4/xml-apis.jar, java.vendor=Sun Microsystems Inc., file.separator=/, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}


1.5.0_08

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值