Class MethodInvokingBean
- java.lang.Object
- org.springframework.util.MethodInvoker
- org.springframework.beans.support.ArgumentConvertingMethodInvoker
- org.springframework.beans.factory.config.MethodInvokingBean
- All Implemented Interfaces:
Aware
,BeanClassLoaderAware
,BeanFactoryAware
,InitializingBean
- Direct Known Subclasses:
MethodInvokingFactoryBean
public class MethodInvokingBean extends ArgumentConvertingMethodInvoker implements BeanClassLoaderAware, BeanFactoryAware, InitializingBean
Simple method invoker bean: just invoking a target method, not expecting a result to expose to the container (in contrast toMethodInvokingFactoryBean
).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, withtargetClass
specifying the Class that the static method is defined on. Alternatively, a target instance method may be specified, by setting thetargetObject
property as the target object, and thetargetMethod
property as the name of the method to call on that target object. Arguments for the method invocation may be specified by setting thearguments
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 initialization method:
<bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingBean"> <property name="staticMethod" value="com.whatever.MyClass.init"/> </bean>
An example of calling an instance method to start some server bean:
<bean id="myStarter" class="org.springframework.beans.factory.config.MethodInvokingBean"> <property name="targetObject" ref="myServer"/> <property name="targetMethod" value="start"/> </bean>
- Since:
- 4.0.3
- Author:
- Juergen Hoeller
- See Also:
MethodInvokingFactoryBean
,MethodInvoker
Field Summary
Fields inherited from class org.springframework.util.MethodInvoker
targetClass
Constructor Summary
Constructors Constructor Description MethodInvokingBean()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected TypeConverter
getDefaultTypeConverter()
Obtain the TypeConverter from the BeanFactory that this bean runs in, if possible.protected Object
invokeWithTargetException()
Perform the invocation and convert InvocationTargetException into the underlying target exception.protected Class<?>
resolveClassName(String className)
Resolve the given class name into a Class.void
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the beanclass loader
to a bean instance.void
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.Methods inherited from class org.springframework.beans.support.ArgumentConvertingMethodInvoker
doFindMatchingMethod, findMatchingMethod, getTypeConverter, registerCustomEditor, setTypeConverter
Methods inherited from class org.springframework.util.MethodInvoker
getArguments, getPreparedMethod, getTargetClass, getTargetMethod, getTargetObject, getTypeDifferenceWeight, invoke, isPrepared, prepare, setArguments, setStaticMethod, setTargetClass, setTargetMethod, setTargetObject
Constructor Detail
MethodInvokingBean
public MethodInvokingBean()
Method Detail
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
Description copied from interface:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- Specified by:
setBeanClassLoader
in interfaceBeanClassLoaderAware
- Parameters:
classLoader
- the owning class loader
resolveClassName
protected Class<?> resolveClassName(String className) throws ClassNotFoundException
Description copied from class:MethodInvoker
Resolve the given class name into a Class.The default implementations uses
ClassUtils.forName
, using the thread context class loader.- Overrides:
resolveClassName
in classMethodInvoker
- Parameters:
className
- the class name to resolve- Returns:
- the resolved Class
- Throws:
ClassNotFoundException
- if the class name was invalid
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
Description copied from interface:BeanFactoryAware
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
InitializingBean.afterPropertiesSet()
or a custom init-method.- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Parameters:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- See Also:
BeanInitializationException
getDefaultTypeConverter
protected TypeConverter getDefaultTypeConverter()
Obtain the TypeConverter from the BeanFactory that this bean runs in, if possible.- Overrides:
getDefaultTypeConverter
in classArgumentConvertingMethodInvoker
- See Also:
ConfigurableBeanFactory.getTypeConverter()
afterPropertiesSet
public void afterPropertiesSet() throws Exception
Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Throws:
Exception
- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
invokeWithTargetException
@Nullable protected Object invokeWithTargetException() throws Exception
Perform the invocation and convert InvocationTargetException into the underlying target exception.- Throws:
Exception