有两种用法,一种是直接吧方法的返回值注入到实例的属性,二是用返回值定义成Bean
定义实体:
配置文件:
<?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>