您的位置:首页 > 编程语言 > Java开发

Spring MethodInvokingFactoryBean注入Method值

2017-10-11 17:14 417 查看
原文地址: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: