spring提供的了filed的值注入和method的返回值注入。
filed值注入需要使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean。在配置文件中需要设置该类。然后再配置其需要注入的类的filed.例如
<beans>
<bean id="son" class="Bean.superIOCfield.Son">
<property name="age">
<bean id="Bean.superIOCfield.Field.TEST_FIELD" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"></bean>
</property>
</bean>
</beans>
其结果就是将Bean.superIOCfield.Field.TEST_FIELD的值注入给了age.
也可以通过配置targetObject和targetMethod属性来实现上面的配置,如果是静态的Filed则配置targetClass和targetMethod或者如下配置:
<bean id="max-long" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField">
<value>java.lang.Long.MAX_VALUE</value>
</property>
</bean>
Method返回值注入需要依赖注入MethodInvokingFactoryBean需要在xml中设定targetObject和targetMethod来指定目标bean和方法如果使用静态方法,则需要指定targetClass和targetMethod
配置文件如下:
<bean id="bdog" class="com.spring.bible.ch3.Dog">
<property name="age">
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="randomAge"/>
</property>
<property name="targetMethod">
<value>getAge</value>
</property>
</bean>
</property>
</bean>
静态时:
<bean id="bdog" class="com.spring.bible.ch3.Dog">
<property name="age">
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass">
<value>com.spring.bible.ch3.RandomAge</value>
</property>
<property name="targetMethod">
<!-- getAge必须是静态方法 -->
<value>getAges</value>
</property>
</bean>
</property>
</bean>