public class

MethodInvokingFactoryBean

extends ArgumentConvertingMethodInvoker
implements BeanClassLoaderAware BeanFactoryAware FactoryBean<T> InitializingBean
java.lang.Object
   ↳ org.springframework.util.MethodInvoker
     ↳ org.springframework.beans.support.ArgumentConvertingMethodInvoker
       ↳ org.springframework.beans.factory.config.MethodInvokingFactoryBean

Class Overview

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.

A static target 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</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>

Summary

Public Constructors
MethodInvokingFactoryBean()
Public Methods
void afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
Object getObject()
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.
Class<?> getObjectType()
Return the type of object that this FactoryBean creates, or null if not known in advance.
boolean isSingleton()
Is the object managed by this factory a singleton? That is, will getObject() always return the same object (a reference that can be cached)?

NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory.

void setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean class loader to a bean instance.
void setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
void setSingleton(boolean singleton)
Set if a singleton should be created, or a new object on each request else.
Protected Methods
TypeConverter getDefaultTypeConverter()
Obtain the TypeConverter from the BeanFactory that this bean runs in, if possible.
Class resolveClassName(String className)
Resolve the given class name into a Class.
[Expand]
Inherited Methods
From class org.springframework.beans.support.ArgumentConvertingMethodInvoker
From class org.springframework.util.MethodInvoker
From class java.lang.Object
From interface org.springframework.beans.factory.BeanClassLoaderAware
From interface org.springframework.beans.factory.BeanFactoryAware
From interface org.springframework.beans.factory.FactoryBean
From interface org.springframework.beans.factory.InitializingBean

Public Constructors

public MethodInvokingFactoryBean ()

Also: SpringBeans

Public Methods

public void afterPropertiesSet ()

Also: SpringBeans

Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Throws
Exception

public Object getObject ()

Also: SpringBeans

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.

Returns
  • an instance of the bean (can be null)
Throws
Exception

public Class<?> getObjectType ()

Also: SpringBeans

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

Returns
  • the type of object that this FactoryBean creates, or null if not known at the time of the call

public boolean isSingleton ()

Also: SpringBeans

Is the object managed by this factory a singleton? That is, will getObject() always return the same object (a reference that can be cached)?

NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

NOTE: This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

Returns
  • whether the exposed object is a singleton

public void setBeanClassLoader (ClassLoader classLoader)

Also: SpringBeans

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's afterPropertiesSet() method or a custom init-method.

Parameters
classLoader the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via getDefaultClassLoader()

public void setBeanFactory (BeanFactory beanFactory)

Also: SpringBeans

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as afterPropertiesSet() or a custom init-method.

Parameters
beanFactory owning BeanFactory (never null). The bean can immediately call methods on the factory.

public void setSingleton (boolean singleton)

Also: SpringBeans

Set if a singleton should be created, or a new object on each request else. Default is "true".

Protected Methods

protected TypeConverter getDefaultTypeConverter ()

Also: SpringBeans

Obtain the TypeConverter from the BeanFactory that this bean runs in, if possible.

protected Class resolveClassName (String className)

Also: SpringBeans

Resolve the given class name into a Class.

The default implementations uses ClassUtils.forName, using the thread context class loader.

Parameters
className the class name to resolve
Returns
  • the resolved Class