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, BeanClassLoaderAware, BeanFactoryAware, InitializingBean { +public class MethodInvokingFactoryBean extends MethodInvokingBean implements FactoryBean { private boolean singleton = true; - private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); - - private ConfigurableBeanFactory beanFactory; - private boolean initialized = false; /** Method call result in the singleton case */ @@ -104,75 +92,18 @@ public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker /** * Set if a singleton should be created, or a new object on each - * request else. Default is "true". + * {@link #getObject()} request otherwise. Default is "true". */ public void setSingleton(boolean singleton) { this.singleton = singleton; } - @Override - public boolean isSingleton() { - return this.singleton; - } - - @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(); if (this.singleton) { this.initialized = true; - this.singletonObject = doInvoke(); - } - } - - /** - * Perform the invocation and convert InvocationTargetException - * into the underlying target exception. - */ - private Object doInvoke() 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; + this.singletonObject = invokeWithTargetException(); } } @@ -193,7 +124,7 @@ public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker } else { // Prototype: new object on each call. - return doInvoke(); + return invokeWithTargetException(); } } @@ -210,4 +141,9 @@ public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker return getPreparedMethod().getReturnType(); } + @Override + public boolean isSingleton() { + return this.singleton; + } + } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java index a35b1e2948..4d5b39e569 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.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,26 +16,27 @@ package org.springframework.beans.factory.config; -import static org.junit.Assert.*; - import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.junit.Test; + import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.support.ArgumentConvertingMethodInvoker; import org.springframework.util.MethodInvoker; +import static org.junit.Assert.*; + /** - * Unit tests for {@link MethodInvokingFactoryBean}. + * Unit tests for {@link MethodInvokingFactoryBean} and {@link MethodInvokingBean}. * * @author Colin Sampaleanu * @author Juergen Hoeller * @author Chris Beams * @since 21.11.2003 */ -public final class MethodInvokingFactoryBeanTests { +public class MethodInvokingFactoryBeanTests { @Test public void testParameterValidation() throws Exception { @@ -246,7 +247,7 @@ public final class MethodInvokingFactoryBeanTests { mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes"); - mcfb.setArguments(new Object[] {new Integer(1), new Object()}); + mcfb.setArguments(new Object[] {1, new Object()}); try { mcfb.afterPropertiesSet(); mcfb.getObject(); @@ -291,7 +292,7 @@ public final class MethodInvokingFactoryBeanTests { ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArgument"); - methodInvoker.setArguments(new Object[] {new Integer(5)}); + methodInvoker.setArguments(new Object[] {5}); methodInvoker.prepare(); methodInvoker.invoke(); @@ -305,49 +306,44 @@ public final class MethodInvokingFactoryBeanTests { @Test public void testInvokeWithIntArguments() throws Exception { - ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); + MethodInvokingBean methodInvoker = new MethodInvokingBean(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArguments"); - methodInvoker.setArguments(new Object[] {new Integer[] {new Integer(5), new Integer(10)}}); - methodInvoker.prepare(); - methodInvoker.invoke(); + methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}}); + methodInvoker.afterPropertiesSet(); - methodInvoker = new ArgumentConvertingMethodInvoker(); + methodInvoker = new MethodInvokingBean(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArguments"); - methodInvoker.setArguments(new Object[] {new String[] {"5", "10"}}); - methodInvoker.prepare(); - methodInvoker.invoke(); + methodInvoker.setArguments(new Object[]{new String[]{"5", "10"}}); + methodInvoker.afterPropertiesSet(); - methodInvoker = new ArgumentConvertingMethodInvoker(); + methodInvoker = new MethodInvokingBean(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArguments"); - methodInvoker.setArguments(new Integer[] {new Integer(5), new Integer(10)}); - methodInvoker.prepare(); - methodInvoker.invoke(); + methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}}); + methodInvoker.afterPropertiesSet(); - methodInvoker = new ArgumentConvertingMethodInvoker(); + methodInvoker = new MethodInvokingBean(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArguments"); - methodInvoker.setArguments(new String[] {"5", "10"}); - methodInvoker.prepare(); - methodInvoker.invoke(); + methodInvoker.setArguments(new String[]{"5", "10"}); + methodInvoker.afterPropertiesSet(); - methodInvoker = new ArgumentConvertingMethodInvoker(); + methodInvoker = new MethodInvokingBean(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArguments"); - methodInvoker.setArguments(new Object[] {new Integer(5), new Integer(10)}); - methodInvoker.prepare(); - methodInvoker.invoke(); + methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}}); + methodInvoker.afterPropertiesSet(); - methodInvoker = new ArgumentConvertingMethodInvoker(); + methodInvoker = new MethodInvokingBean(); methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetMethod("intArguments"); - methodInvoker.setArguments(new Object[] {"5", "10"}); - methodInvoker.prepare(); - methodInvoker.invoke(); + methodInvoker.setArguments(new Object[]{"5", "10"}); + methodInvoker.afterPropertiesSet(); } + public static class TestClass1 { public static int _staticField1; @@ -395,5 +391,4 @@ public final class MethodInvokingFactoryBeanTests { } } - }