Extracted simple MethodInvokingBean as alternative to (and base class for) MethodInvokingFactoryBean

Issue: SPR-11196
This commit is contained in:
Juergen Hoeller 2014-03-17 15:13:59 +01:00
parent a8577da30c
commit cf290ab42a
3 changed files with 185 additions and 122 deletions

View File

@ -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}).
*
* <p>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.
*
* <p>This class depends on {@link #afterPropertiesSet()} being called once
* all properties have been set, as per the InitializingBean contract.
*
* <p>An example (in an XML based bean factory definition) of a bean definition
* which uses this class to call a static initialization method:
*
* <pre class="code">
* &lt;bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingBean">
* &lt;property name="staticMethod" value="com.whatever.MyClass.init"/>
* &lt;/bean></pre>
*
* <p>An example of calling an instance method to start some server bean:
*
* <pre class="code">
* &lt;bean id="myStarter" class="org.springframework.beans.factory.config.MethodInvokingBean">
* &lt;property name="targetObject" ref="myServer"/>
* &lt;property name="targetMethod" value="start"/>
* &lt;/bean></pre>
*
* @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;
}
}
}

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,17 +16,8 @@
package org.springframework.beans.factory.config; 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.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException; 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 * {@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 * {@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. * factory to invoke the target method each time it is asked for an object.
* *
* <p>A static target method may be specified by setting the * <p><b>NOTE: If your target method does not produce a result to expose, consider
* {@link #setTargetMethod targetMethod} property to a String representing the static * {@link MethodInvokingBean} instead, which avoids the type determination and
* method name, with {@link #setTargetClass targetClass} specifying the Class that * lifecycle limitations that this {@link MethodInvokingFactoryBean} comes with.</b>
* the static method is defined on. Alternatively, a target instance method may be *
* <p>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 * specified, by setting the {@link #setTargetObject targetObject} property as the target
* object, and the {@link #setTargetMethod targetMethod} property as the name of the * 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 * method to call on that target object. Arguments for the method invocation may be
@ -61,7 +56,7 @@ import org.springframework.util.ClassUtils;
* *
* <pre class="code"> * <pre class="code">
* &lt;bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> * &lt;bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
* &lt;property name="staticMethod">&lt;value>com.whatever.MyClassFactory.getInstance&lt;/value>&lt;/property> * &lt;property name="staticMethod" value="com.whatever.MyClassFactory.getInstance"/>
* &lt;/bean></pre> * &lt;/bean></pre>
* *
* <p>An example of calling a static method then an instance method to get at a * <p>An example of calling a static method then an instance method to get at a
@ -69,33 +64,26 @@ import org.springframework.util.ClassUtils;
* *
* <pre class="code"> * <pre class="code">
* &lt;bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> * &lt;bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
* &lt;property name="targetClass">&lt;value>java.lang.System&lt;/value>&lt;/property> * &lt;property name="targetClass" value="java.lang.System"/>
* &lt;property name="targetMethod">&lt;value>getProperties&lt;/value>&lt;/property> * &lt;property name="targetMethod" value="getProperties"/>
* &lt;/bean> * &lt;/bean>
* *
* &lt;bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> * &lt;bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
* &lt;property name="targetObject">&lt;ref local="sysProps"/>&lt;/property> * &lt;property name="targetObject" value="sysProps"/>
* &lt;property name="targetMethod">&lt;value>getProperty&lt;/value>&lt;/property> * &lt;property name="targetMethod" value="getProperty"/>
* &lt;property name="arguments"> * &lt;property name="arguments" value="java.version"/>
* &lt;list>
* &lt;value>java.version&lt;/value>
* &lt;/list>
* &lt;/property>
* &lt;/bean></pre> * &lt;/bean></pre>
* *
* @author Colin Sampaleanu * @author Colin Sampaleanu
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 21.11.2003 * @since 21.11.2003
* @see MethodInvokingBean
* @see org.springframework.util.MethodInvoker
*/ */
public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker public class MethodInvokingFactoryBean extends MethodInvokingBean implements FactoryBean<Object> {
implements FactoryBean<Object>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean {
private boolean singleton = true; private boolean singleton = true;
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
private ConfigurableBeanFactory beanFactory;
private boolean initialized = false; private boolean initialized = false;
/** Method call result in the singleton case */ /** 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 * 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) { public void setSingleton(boolean singleton) {
this.singleton = 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 @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
prepare(); prepare();
if (this.singleton) { if (this.singleton) {
this.initialized = true; this.initialized = true;
this.singletonObject = doInvoke(); this.singletonObject = invokeWithTargetException();
}
}
/**
* 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;
} }
} }
@ -193,7 +124,7 @@ public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker
} }
else { else {
// Prototype: new object on each call. // Prototype: new object on each call.
return doInvoke(); return invokeWithTargetException();
} }
} }
@ -210,4 +141,9 @@ public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker
return getPreparedMethod().getReturnType(); return getPreparedMethod().getReturnType();
} }
@Override
public boolean isSingleton() {
return this.singleton;
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,26 +16,27 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.beans.support.ArgumentConvertingMethodInvoker; import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
import org.springframework.util.MethodInvoker; 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 Colin Sampaleanu
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
* @since 21.11.2003 * @since 21.11.2003
*/ */
public final class MethodInvokingFactoryBeanTests { public class MethodInvokingFactoryBeanTests {
@Test @Test
public void testParameterValidation() throws Exception { public void testParameterValidation() throws Exception {
@ -246,7 +247,7 @@ public final class MethodInvokingFactoryBeanTests {
mcfb = new MethodInvokingFactoryBean(); mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes"); mcfb.setTargetMethod("supertypes");
mcfb.setArguments(new Object[] {new Integer(1), new Object()}); mcfb.setArguments(new Object[] {1, new Object()});
try { try {
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
mcfb.getObject(); mcfb.getObject();
@ -291,7 +292,7 @@ public final class MethodInvokingFactoryBeanTests {
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArgument"); methodInvoker.setTargetMethod("intArgument");
methodInvoker.setArguments(new Object[] {new Integer(5)}); methodInvoker.setArguments(new Object[] {5});
methodInvoker.prepare(); methodInvoker.prepare();
methodInvoker.invoke(); methodInvoker.invoke();
@ -305,49 +306,44 @@ public final class MethodInvokingFactoryBeanTests {
@Test @Test
public void testInvokeWithIntArguments() throws Exception { public void testInvokeWithIntArguments() throws Exception {
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); MethodInvokingBean methodInvoker = new MethodInvokingBean();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArguments"); methodInvoker.setTargetMethod("intArguments");
methodInvoker.setArguments(new Object[] {new Integer[] {new Integer(5), new Integer(10)}}); methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}});
methodInvoker.prepare(); methodInvoker.afterPropertiesSet();
methodInvoker.invoke();
methodInvoker = new ArgumentConvertingMethodInvoker(); methodInvoker = new MethodInvokingBean();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArguments"); methodInvoker.setTargetMethod("intArguments");
methodInvoker.setArguments(new Object[] {new String[] {"5", "10"}}); methodInvoker.setArguments(new Object[]{new String[]{"5", "10"}});
methodInvoker.prepare(); methodInvoker.afterPropertiesSet();
methodInvoker.invoke();
methodInvoker = new ArgumentConvertingMethodInvoker(); methodInvoker = new MethodInvokingBean();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArguments"); methodInvoker.setTargetMethod("intArguments");
methodInvoker.setArguments(new Integer[] {new Integer(5), new Integer(10)}); methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}});
methodInvoker.prepare(); methodInvoker.afterPropertiesSet();
methodInvoker.invoke();
methodInvoker = new ArgumentConvertingMethodInvoker(); methodInvoker = new MethodInvokingBean();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArguments"); methodInvoker.setTargetMethod("intArguments");
methodInvoker.setArguments(new String[] {"5", "10"}); methodInvoker.setArguments(new String[]{"5", "10"});
methodInvoker.prepare(); methodInvoker.afterPropertiesSet();
methodInvoker.invoke();
methodInvoker = new ArgumentConvertingMethodInvoker(); methodInvoker = new MethodInvokingBean();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArguments"); methodInvoker.setTargetMethod("intArguments");
methodInvoker.setArguments(new Object[] {new Integer(5), new Integer(10)}); methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}});
methodInvoker.prepare(); methodInvoker.afterPropertiesSet();
methodInvoker.invoke();
methodInvoker = new ArgumentConvertingMethodInvoker(); methodInvoker = new MethodInvokingBean();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
methodInvoker.setTargetMethod("intArguments"); methodInvoker.setTargetMethod("intArguments");
methodInvoker.setArguments(new Object[] {"5", "10"}); methodInvoker.setArguments(new Object[]{"5", "10"});
methodInvoker.prepare(); methodInvoker.afterPropertiesSet();
methodInvoker.invoke();
} }
public static class TestClass1 { public static class TestClass1 {
public static int _staticField1; public static int _staticField1;
@ -395,5 +391,4 @@ public final class MethodInvokingFactoryBeanTests {
} }
} }
} }