1 16 17 package org.springframework.beans.factory.config; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import org.springframework.beans.TypeConverter; 23 import org.springframework.beans.factory.BeanClassLoaderAware; 24 import org.springframework.beans.factory.BeanFactory; 25 import org.springframework.beans.factory.BeanFactoryAware; 26 import org.springframework.beans.factory.FactoryBean; 27 import org.springframework.beans.factory.FactoryBeanNotInitializedException; 28 import org.springframework.beans.factory.InitializingBean; 29 import org.springframework.beans.support.ArgumentConvertingMethodInvoker; 30 import org.springframework.util.ClassUtils; 31 32 91 public class MethodInvokingFactoryBean extends ArgumentConvertingMethodInvoker 92 implements FactoryBean, BeanClassLoaderAware, BeanFactoryAware, InitializingBean { 93 94 private boolean singleton = true; 95 96 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 97 98 private ConfigurableBeanFactory beanFactory; 99 100 private boolean initialized = false; 101 102 103 private Object singletonObject; 104 105 106 110 public void setSingleton(boolean singleton) { 111 this.singleton = singleton; 112 } 113 114 public boolean isSingleton() { 115 return this.singleton; 116 } 117 118 public void setBeanClassLoader(ClassLoader classLoader) { 119 this.beanClassLoader = classLoader; 120 } 121 122 protected Class resolveClassName(String className) throws ClassNotFoundException { 123 return ClassUtils.forName(className, this.beanClassLoader); 124 } 125 126 public void setBeanFactory(BeanFactory beanFactory) { 127 if (beanFactory instanceof ConfigurableBeanFactory) { 128 this.beanFactory = (ConfigurableBeanFactory) beanFactory; 129 } 130 } 131 132 137 protected TypeConverter getDefaultTypeConverter() { 138 if (this.beanFactory != null) { 139 return this.beanFactory.getTypeConverter(); 140 } 141 else { 142 return super.getDefaultTypeConverter(); 143 } 144 } 145 146 147 public void afterPropertiesSet() throws Exception { 148 prepare(); 149 if (this.singleton) { 150 this.initialized = true; 151 this.singletonObject = doInvoke(); 152 } 153 } 154 155 159 private Object doInvoke() throws Exception { 160 try { 161 return invoke(); 162 } 163 catch (InvocationTargetException ex) { 164 if (ex.getTargetException() instanceof Exception ) { 165 throw (Exception ) ex.getTargetException(); 166 } 167 if (ex.getTargetException() instanceof Error ) { 168 throw (Error ) ex.getTargetException(); 169 } 170 throw ex; 171 } 172 } 173 174 175 180 public Object getObject() throws Exception { 181 if (this.singleton) { 182 if (!this.initialized) { 183 throw new FactoryBeanNotInitializedException(); 184 } 185 return this.singletonObject; 187 } 188 else { 189 return doInvoke(); 191 } 192 } 193 194 198 public Class getObjectType() { 199 Method preparedMethod = getPreparedMethod(); 200 if (preparedMethod == null) { 201 return null; 203 } 204 return preparedMethod.getReturnType(); 205 } 206 207 } 208 | Popular Tags |