站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 1.2.9 API 文档英文版

MethodInvokingFactoryBean (Spring Framework) - Spring Framework 1.2.9 API 文档英文版


org.springframework.beans.factory.config
Class MethodInvokingFactoryBean

java.lang.Object
  extended by org.springframework.util.MethodInvoker
      extended by org.springframework.beans.support.ArgumentConvertingMethodInvoker
          extended by org.springframework.beans.factory.config.MethodInvokingFactoryBean
All Implemented Interfaces:
FactoryBean, InitializingBean

public class MethodInvokingFactoryBean
extends ArgumentConvertingMethodInvoker
implements FactoryBean, InitializingBean

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 become the bean.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.

Note that this factory bean will return the special MethodInvoker.VOID singleton instance when it is used to invoke a method which returns null, or has a void return type. While the user of the factory bean is presumably calling the method to perform some sort of initialization, and doesn't care about any return value, all factory beans must return a value, so this special singleton instance is used for this case.

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>

Since:
21.11.2003
Author:
Colin Sampaleanu, Juergen Hoeller

Nested Class Summary
 
Nested classes/interfaces 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).
 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.
 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?
 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.beans.support.ArgumentConvertingMethodInvoker
findMatchingMethod, registerCustomEditor
 
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

MethodInvokingFactoryBean

public MethodInvokingFactoryBean()
Method Detail

setSingleton

public void setSingleton(boolean singleton)
Set if a singleton should be created, or a new object on each request else. Default is "true".


afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Description copied from interface: InitializingBean
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.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

getObject

public Object getObject()
                 throws Exception
Returns the same value each time if the singleton property is set to true, otherwise returns the value returned from invoking the specified method. However, returns MethodInvoker.VOID if the method returns null or has a void return type, since factory beans must return a result.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should not be null; a null value will be considered as an indication of incomplete initialization)
Throws:
Exception - in case of creation errors
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance. This allows to check for specific types of beans without instantiating objects, for example on autowiring.

For a singleton, this should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? 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.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton
See Also:
FactoryBean.getObject()


Copyright (c) 2002-2007 The Spring Framework Project.