spring-framework / org.springframework.beans.factory.config / MethodInvokingFactoryBean

MethodInvokingFactoryBean

open class MethodInvokingFactoryBean : MethodInvokingBean, FactoryBean<Any>

FactoryBean which returns a value which is the result of a static or instance method invocation. For most use cases it is better to just use the container's built-in factory method support for the same purpose, since that is smarter at converting arguments. This factory bean is still useful though when you need to call a method which doesn't return any value (for example, a static class method to force some sort of initialization to happen). This use case is not supported by factory methods, since a return value is needed to obtain the bean instance.

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 #getObject by the owning bean factory will cause a method invocation, whose return value will be cached for subsequent requests. An internal singleton property may be set to "false", to cause this factory to invoke the target method each time it is asked for an object.

NOTE: If your target method does not produce a result to expose, consider MethodInvokingBean instead, which avoids the type determination and lifecycle limitations that this MethodInvokingFactoryBean comes with.

This invoker supports any kind of target method. A static method may be specified by setting the targetMethod property to a String representing the static method name, with targetClass specifying the Class that the static method is defined on. Alternatively, a target instance method may be specified, by setting the targetObject property as the target object, and the targetMethod property as the name of the method to call on that target object. Arguments for the method invocation may be specified by setting the arguments property.

This class depends on #afterPropertiesSet() being called once all 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"/> </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"/> <property name="targetMethod" value="getProperties"/> </bean> <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="sysProps"/> <property name="targetMethod" value="getProperty"/> <property name="arguments" value="java.version"/> </bean>

Author
Colin Sampaleanu

Author
Juergen Hoeller

Since
21.11.2003

See Also
MethodInvokingBeanorg.springframework.util.MethodInvoker

Constructors

<init>

MethodInvokingFactoryBean()

FactoryBean which returns a value which is the result of a static or instance method invocation. For most use cases it is better to just use the container's built-in factory method support for the same purpose, since that is smarter at converting arguments. This factory bean is still useful though when you need to call a method which doesn't return any value (for example, a static class method to force some sort of initialization to happen). This use case is not supported by factory methods, since a return value is needed to obtain the bean instance.

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 #getObject by the owning bean factory will cause a method invocation, whose return value will be cached for subsequent requests. An internal singleton property may be set to "false", to cause this factory to invoke the target method each time it is asked for an object.

NOTE: If your target method does not produce a result to expose, consider MethodInvokingBean instead, which avoids the type determination and lifecycle limitations that this MethodInvokingFactoryBean comes with.

This invoker supports any kind of target method. A static method may be specified by setting the targetMethod property to a String representing the static method name, with targetClass specifying the Class that the static method is defined on. Alternatively, a target instance method may be specified, by setting the targetObject property as the target object, and the targetMethod property as the name of the method to call on that target object. Arguments for the method invocation may be specified by setting the arguments property.

This class depends on #afterPropertiesSet() being called once all 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"/> </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"/> <property name="targetMethod" value="getProperties"/> </bean> <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="sysProps"/> <property name="targetMethod" value="getProperty"/> <property name="arguments" value="java.version"/> </bean>

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

getObject

open fun getObject(): Any

Returns the same value each time if the singleton property is set to "true", otherwise returns the value returned from invoking the specified method on the fly.

getObjectType

open fun getObjectType(): Class<*>

Return the type of object that this FactoryBean creates, or null if not known in advance.

isSingleton

open fun isSingleton(): Boolean

setSingleton

open fun setSingleton(singleton: Boolean): Unit

Set if a singleton should be created, or a new object on each #getObject() request otherwise. Default is "true".

Inherited Functions

setBeanClassLoader

open fun setBeanClassLoader(classLoader: ClassLoader): Unit

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit