org.springframework.beans.factory.config
Class MethodInvokingFactoryBean
- public class MethodInvokingFactoryBean
- implements FactoryBean, InitializingBean
FactoryBean which returns a value which is the result of a static or instance
method invocation.
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
staticMethod property to a String representing the fully
qualified static method name. Alternately, a target instance method may be
specified, by setting the target 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 args 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="myClass" 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="staticMethod"><value>java.lang.System.getProperties</value></property>
</bean>
<bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="target"><ref local='sysProps'/></property>
<property name="targetMethod"><value>getProperty</value></property>
<property name="args">
<list>
<value>|java.version|</value>
</list>
</property>
</bean>
- Author:
- colin sampaleanu
- Version: $Id: MethodInvokingFactoryBean.java,v 1.4 2003/11/26 19:35:30 colins Exp $
- Since: 2003-11-21
VOID
public final static VoidType VOID
afterPropertiesSet
public void afterPropertiesSet()
throws java.lang.IllegalArgumentException,
org.springframework.beans.BeansException
getArgs
public Object[] getArgs()
getObject
public Object getObject()
throws java.lang.Exception
getObjectType
public Class getObjectType()
getStaticMethod
public String getStaticMethod()
getTarget
public Object getTarget()
getTargetMethod
public String getTargetMethod()
isSingleton
public boolean isSingleton()
setArgs
public void setArgs(Object[] args)
- Allows arguments for the method invocation to be specified. If this property
is not set (null), or the Object array is of length 0, a method with no
arguments is assumed.
setSingleton
public void setSingleton(boolean singleton)
- Set if a singleton should be created, or a new object on each request
else. Default is true.
setStaticMethod
public void setStaticMethod(String staticMethod)
- The fully qualified name of the static method to call (for example,
java.lang.System.getProperties. If this property is non-null, then
the target property must be null; they are mutually exclusive.
setTarget
public void setTarget(Object target)
- Set the target object on which to call the target method. If this property is
non-null, then the staticMethod property must be null; they are mutually
exclusive.
setTargetMethod
public void setTargetMethod(String targetMethod)
- When the target property has been set, specifies the name of the method on that
object which should be invoked. Not used for static method invocations.
to Class org.springframework.beans.factory.config.MethodInvokingFactoryBean.VoidType
to Class java.lang.String
to Class java.lang.String
to Class java.lang.reflect.Method