diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingBean.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingBean.java new file mode 100644 index 0000000000..cd1ad28708 --- /dev/null +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingBean.java @@ -0,0 +1,132 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory.config; + +import java.lang.reflect.InvocationTargetException; + +import org.springframework.beans.TypeConverter; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.support.ArgumentConvertingMethodInvoker; +import org.springframework.util.ClassUtils; + +/** + * Simple method invoker bean: just invoking a target method, not expecting a result + * to expose to the container (in contrast to {@link MethodInvokingFactoryBean}). + * + *
This invoker supports any kind of target method. A static method may be specified + * by setting the {@link #setTargetMethod targetMethod} property to a String representing + * the static method name, with {@link #setTargetClass targetClass} specifying the Class + * that the static method is defined on. Alternatively, a target instance method may be + * specified, by setting the {@link #setTargetObject targetObject} property as the target + * object, and the {@link #setTargetMethod 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 {@link #setArguments arguments} property. + * + *
This class depends on {@link #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>+ * + * @author Juergen Hoeller + * @since 4.0.3 + * @see MethodInvokingFactoryBean + * @see org.springframework.util.MethodInvoker + */ +public class MethodInvokingBean extends ArgumentConvertingMethodInvoker + implements BeanClassLoaderAware, BeanFactoryAware, InitializingBean { + + private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); + + private ConfigurableBeanFactory beanFactory; + + + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + + @Override + protected Class> resolveClassName(String className) throws ClassNotFoundException { + return ClassUtils.forName(className, this.beanClassLoader); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) { + if (beanFactory instanceof ConfigurableBeanFactory) { + this.beanFactory = (ConfigurableBeanFactory) beanFactory; + } + } + + /** + * Obtain the TypeConverter from the BeanFactory that this bean runs in, + * if possible. + * @see ConfigurableBeanFactory#getTypeConverter() + */ + @Override + protected TypeConverter getDefaultTypeConverter() { + if (this.beanFactory != null) { + return this.beanFactory.getTypeConverter(); + } + else { + return super.getDefaultTypeConverter(); + } + } + + + @Override + public void afterPropertiesSet() throws Exception { + prepare(); + invokeWithTargetException(); + } + + /** + * Perform the invocation and convert InvocationTargetException + * into the underlying target exception. + */ + protected Object invokeWithTargetException() throws Exception { + try { + return invoke(); + } + catch (InvocationTargetException ex) { + if (ex.getTargetException() instanceof Exception) { + throw (Exception) ex.getTargetException(); + } + if (ex.getTargetException() instanceof Error) { + throw (Error) ex.getTargetException(); + } + throw ex; + } + } + +} diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingFactoryBean.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingFactoryBean.java index 505afb8223..fe3f25b47b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingFactoryBean.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/MethodInvokingFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,17 +16,8 @@ package org.springframework.beans.factory.config; -import java.lang.reflect.InvocationTargetException; - -import org.springframework.beans.TypeConverter; -import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBeanNotInitializedException; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.support.ArgumentConvertingMethodInvoker; -import org.springframework.util.ClassUtils; /** * {@link FactoryBean} which returns a value which is the result of a static or instance @@ -44,10 +35,14 @@ import org.springframework.util.ClassUtils; * {@link #setSingleton 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 - * {@link #setTargetMethod targetMethod} property to a String representing the static - * method name, with {@link #setTargetClass targetClass} specifying the Class that - * the static method is defined on. Alternatively, a target instance method may be + *
NOTE: If your target method does not produce a result to expose, consider + * {@link MethodInvokingBean} instead, which avoids the type determination and + * lifecycle limitations that this {@link MethodInvokingFactoryBean} comes with. + * + *
This invoker supports any kind of target method. A static method may be specified + * by setting the {@link #setTargetMethod targetMethod} property to a String representing + * the static method name, with {@link #setTargetClass targetClass} specifying the Class + * that the static method is defined on. Alternatively, a target instance method may be * specified, by setting the {@link #setTargetObject targetObject} property as the target * object, and the {@link #setTargetMethod targetMethod} property as the name of the * method to call on that target object. Arguments for the method invocation may be @@ -61,7 +56,7 @@ import org.springframework.util.ClassUtils; * *
* <bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> - * <property name="staticMethod"><value>com.whatever.MyClassFactory.getInstance</value></property> + * <property name="staticMethod" value="com.whatever.MyClassFactory.getInstance"/> * </bean>* *
An example of calling a static method then an instance method to get at a @@ -69,33 +64,26 @@ import org.springframework.util.ClassUtils; * *
* <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> + * <property name="targetClass" value="java.lang.System"/> + * <property name="targetMethod" value="getProperties"/> * </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> + * <property name="targetObject" value="sysProps"/> + * <property name="targetMethod" value="getProperty"/> + * <property name="arguments" value="java.version"/> * </bean>* * @author Colin Sampaleanu * @author Juergen Hoeller * @since 21.11.2003 + * @see MethodInvokingBean + * @see org.springframework.util.MethodInvoker */ -public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker - implements FactoryBean