001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.config; 018 019import java.lang.reflect.InvocationTargetException; 020 021import org.springframework.beans.TypeConverter; 022import org.springframework.beans.factory.BeanClassLoaderAware; 023import org.springframework.beans.factory.BeanFactory; 024import org.springframework.beans.factory.BeanFactoryAware; 025import org.springframework.beans.factory.InitializingBean; 026import org.springframework.beans.support.ArgumentConvertingMethodInvoker; 027import org.springframework.util.ClassUtils; 028 029/** 030 * Simple method invoker bean: just invoking a target method, not expecting a result 031 * to expose to the container (in contrast to {@link MethodInvokingFactoryBean}). 032 * 033 * <p>This invoker supports any kind of target method. A static method may be specified 034 * by setting the {@link #setTargetMethod targetMethod} property to a String representing 035 * the static method name, with {@link #setTargetClass targetClass} specifying the Class 036 * that the static method is defined on. Alternatively, a target instance method may be 037 * specified, by setting the {@link #setTargetObject targetObject} property as the target 038 * object, and the {@link #setTargetMethod targetMethod} property as the name of the 039 * method to call on that target object. Arguments for the method invocation may be 040 * specified by setting the {@link #setArguments arguments} property. 041 * 042 * <p>This class depends on {@link #afterPropertiesSet()} being called once 043 * all properties have been set, as per the InitializingBean contract. 044 * 045 * <p>An example (in an XML based bean factory definition) of a bean definition 046 * which uses this class to call a static initialization method: 047 * 048 * <pre class="code"> 049 * <bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingBean"> 050 * <property name="staticMethod" value="com.whatever.MyClass.init"/> 051 * </bean></pre> 052 * 053 * <p>An example of calling an instance method to start some server bean: 054 * 055 * <pre class="code"> 056 * <bean id="myStarter" class="org.springframework.beans.factory.config.MethodInvokingBean"> 057 * <property name="targetObject" ref="myServer"/> 058 * <property name="targetMethod" value="start"/> 059 * </bean></pre> 060 * 061 * @author Juergen Hoeller 062 * @since 4.0.3 063 * @see MethodInvokingFactoryBean 064 * @see org.springframework.util.MethodInvoker 065 */ 066public class MethodInvokingBean extends ArgumentConvertingMethodInvoker 067 implements BeanClassLoaderAware, BeanFactoryAware, InitializingBean { 068 069 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 070 071 private ConfigurableBeanFactory beanFactory; 072 073 074 @Override 075 public void setBeanClassLoader(ClassLoader classLoader) { 076 this.beanClassLoader = classLoader; 077 } 078 079 @Override 080 protected Class<?> resolveClassName(String className) throws ClassNotFoundException { 081 return ClassUtils.forName(className, this.beanClassLoader); 082 } 083 084 @Override 085 public void setBeanFactory(BeanFactory beanFactory) { 086 if (beanFactory instanceof ConfigurableBeanFactory) { 087 this.beanFactory = (ConfigurableBeanFactory) beanFactory; 088 } 089 } 090 091 /** 092 * Obtain the TypeConverter from the BeanFactory that this bean runs in, 093 * if possible. 094 * @see ConfigurableBeanFactory#getTypeConverter() 095 */ 096 @Override 097 protected TypeConverter getDefaultTypeConverter() { 098 if (this.beanFactory != null) { 099 return this.beanFactory.getTypeConverter(); 100 } 101 else { 102 return super.getDefaultTypeConverter(); 103 } 104 } 105 106 107 @Override 108 public void afterPropertiesSet() throws Exception { 109 prepare(); 110 invokeWithTargetException(); 111 } 112 113 /** 114 * Perform the invocation and convert InvocationTargetException 115 * into the underlying target exception. 116 */ 117 protected Object invokeWithTargetException() throws Exception { 118 try { 119 return invoke(); 120 } 121 catch (InvocationTargetException ex) { 122 if (ex.getTargetException() instanceof Exception) { 123 throw (Exception) ex.getTargetException(); 124 } 125 if (ex.getTargetException() instanceof Error) { 126 throw (Error) ex.getTargetException(); 127 } 128 throw ex; 129 } 130 } 131 132}