|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.util.MethodInvoker
org.springframework.beans.factory.config.MethodInvokingFactoryBean
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
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 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="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="args"> <list> <value>|java.version|</value> </list> </property> </bean>
Nested Class Summary |
Nested classes inherited from class org.springframework.util.MethodInvoker |
MethodInvoker.VoidType |
Field Summary |
Fields inherited from class org.springframework.util.MethodInvoker |
VOID |
Constructor Summary | |
MethodInvokingFactoryBean()
|
Method Summary | |
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware). |
java.lang.Object |
getObject()
Return an instance (possibly shared or independent) of the object managed by this factory. |
java.lang.Class |
getObjectType()
Return the type of object that this FactoryBean creates, or null if not known in advance. |
boolean |
isSingleton()
Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object? |
void |
setSingleton(boolean singleton)
Set if a singleton should be created, or a new object on each request else. |
Methods inherited from class org.springframework.util.MethodInvoker |
getArguments, getPreparedMethod, getTargetClass, getTargetMethod, getTargetObject, invoke, prepare, setArguments, setStaticMethod, setTargetClass, setTargetMethod, setTargetObject |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public MethodInvokingFactoryBean()
Method Detail |
public void setSingleton(boolean singleton)
public void afterPropertiesSet() throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException
InitializingBean
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.
afterPropertiesSet
in interface InitializingBean
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
public java.lang.Object getObject() throws java.lang.Exception
FactoryBean
getObject
in interface FactoryBean
java.lang.Exception
- in case of creation errorspublic java.lang.Class getObjectType()
FactoryBean
For a singleton, this can simply return getObject().getClass(), or even null, as autowiring will always check the actual objects for singletons. For prototypes, returning a meaningful type here is highly advisable, as autowiring will simply ignore them else.
getObjectType
in interface FactoryBean
ListableBeanFactory.getBeansOfType(java.lang.Class, boolean, boolean)
public boolean isSingleton()
FactoryBean
The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory.
isSingleton
in interface FactoryBean
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |