Note that as it is expected to be used mostly for accessing factory methods, this factory by default operates in a singleton fashion. The first request to {@link #getObject} by the owning bean factory will cause a method invocation,whose return value will be cached for subsequent requests. An internal {@link #setSingleton singleton} property may be set to "false", to cause thisfactory to invoke the target method each time it is asked for an object.
A static target method may be specified by setting the {@link #setTargetMethod targetMethod} property to a String representing the staticmethod name, with {@link #setTargetClass targetClass} specifying the Class thatthe static method is defined on. Alternatively, a target instance method may be specified, by setting the {@link #setTargetObject targetObject} property as the targetobject, and the {@link #setTargetMethod targetMethod} property as the name of themethod to call on that target object. Arguments for the method invocation may be specified by setting the {@link #setArguments arguments} property.
This class depends on {@link #afterPropertiesSet()} being called onceall properties have been set, as per the InitializingBean contract.
An example (in an XML based bean factory definition) of a bean definition which uses this class to call a static factory method:
<bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod"><value>com.whatever.MyClassFactory.getInstance</value></property> </bean>
An example of calling a static method then an instance method to get at a Java system property. Somewhat verbose, but it works.
<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>@author Colin Sampaleanu @author Juergen Hoeller @since 21.11.2003
|
|
|
|
|
|