001/* 002 * Copyright 2002-2017 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.lang.Nullable; 028import org.springframework.util.ClassUtils; 029 030/** 031 * Simple method invoker bean: just invoking a target method, not expecting a result 032 * to expose to the container (in contrast to {@link MethodInvokingFactoryBean}). 033 * 034 * <p>This invoker supports any kind of target method. A static method may be specified 035 * by setting the {@link #setTargetMethod targetMethod} property to a String representing 036 * the static method name, with {@link #setTargetClass targetClass} specifying the Class 037 * that the static method is defined on. Alternatively, a target instance method may be 038 * specified, by setting the {@link #setTargetObject targetObject} property as the target 039 * object, and the {@link #setTargetMethod targetMethod} property as the name of the 040 * method to call on that target object. Arguments for the method invocation may be 041 * specified by setting the {@link #setArguments arguments} property. 042 * 043 * <p>This class depends on {@link #afterPropertiesSet()} being called once 044 * all properties have been set, as per the InitializingBean contract. 045 * 046 * <p>An example (in an XML based bean factory definition) of a bean definition 047 * which uses this class to call a static initialization method: 048 * 049 * <pre class="code"> 050 * <bean id="myObject" class="org.springframework.beans.factory.config.MethodInvokingBean"> 051 * <property name="staticMethod" value="com.whatever.MyClass.init"/> 052 * </bean></pre> 053 * 054 * <p>An example of calling an instance method to start some server bean: 055 * 056 * <pre class="code"> 057 * <bean id="myStarter" class="org.springframework.beans.factory.config.MethodInvokingBean"> 058 * <property name="targetObject" ref="myServer"/> 059 * <property name="targetMethod" value="start"/> 060 * </bean></pre> 061 * 062 * @author Juergen Hoeller 063 * @since 4.0.3 064 * @see MethodInvokingFactoryBean 065 * @see org.springframework.util.MethodInvoker 066 */ 067public class MethodInvokingBean extends ArgumentConvertingMethodInvoker 068 implements BeanClassLoaderAware, BeanFactoryAware, InitializingBean { 069 070 @Nullable 071 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 072 073 @Nullable 074 private ConfigurableBeanFactory beanFactory; 075 076 077 @Override 078 public void setBeanClassLoader(ClassLoader classLoader) { 079 this.beanClassLoader = classLoader; 080 } 081 082 @Override 083 protected Class<?> resolveClassName(String className) throws ClassNotFoundException { 084 return ClassUtils.forName(className, this.beanClassLoader); 085 } 086 087 @Override 088 public void setBeanFactory(BeanFactory beanFactory) { 089 if (beanFactory instanceof ConfigurableBeanFactory) { 090 this.beanFactory = (ConfigurableBeanFactory) beanFactory; 091 } 092 } 093 094 /** 095 * Obtain the TypeConverter from the BeanFactory that this bean runs in, 096 * if possible. 097 * @see ConfigurableBeanFactory#getTypeConverter() 098 */ 099 @Override 100 protected TypeConverter getDefaultTypeConverter() { 101 if (this.beanFactory != null) { 102 return this.beanFactory.getTypeConverter(); 103 } 104 else { 105 return super.getDefaultTypeConverter(); 106 } 107 } 108 109 110 @Override 111 public void afterPropertiesSet() throws Exception { 112 prepare(); 113 invokeWithTargetException(); 114 } 115 116 /** 117 * Perform the invocation and convert InvocationTargetException 118 * into the underlying target exception. 119 */ 120 @Nullable 121 protected Object invokeWithTargetException() throws Exception { 122 try { 123 return invoke(); 124 } 125 catch (InvocationTargetException ex) { 126 if (ex.getTargetException() instanceof Exception) { 127 throw (Exception) ex.getTargetException(); 128 } 129 if (ex.getTargetException() instanceof Error) { 130 throw (Error) ex.getTargetException(); 131 } 132 throw ex; 133 } 134 } 135 136}