有兩種用法,一種是直接吧方法的返回值注入到實(shí)例的屬性,二是用返回值定義成Bean
定義實(shí)體:
配置文件:
<?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>