org.springframework.beans.factory.config

Class 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 to MethodInvokingFactoryBean).

    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, 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 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
    • Constructor Detail

      • MethodInvokingBean

        public MethodInvokingBean()
    • Method Detail

      • resolveClassName

        protected java.lang.Class<?> resolveClassName(java.lang.String className)
                                               throws java.lang.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 class MethodInvoker
        Parameters:
        className - the class name to resolve
        Returns:
        the resolved Class
        Throws:
        java.lang.ClassNotFoundException - if the class name was invalid
      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Description copied from interface: InitializingBean
        Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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 interface InitializingBean
        Throws:
        java.lang.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 java.lang.Object invokeWithTargetException()
                                                                throws java.lang.Exception
        Perform the invocation and convert InvocationTargetException into the underlying target exception.
        Throws:
        java.lang.Exception