1 16 17 package org.springframework.aop.framework.autoproxy; 18 19 import java.beans.PropertyDescriptor ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import org.aopalliance.aop.Advice; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.springframework.aop.Advisor; 32 import org.springframework.aop.TargetSource; 33 import org.springframework.aop.framework.AopInfrastructureBean; 34 import org.springframework.aop.framework.ProxyConfig; 35 import org.springframework.aop.framework.ProxyFactory; 36 import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry; 37 import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry; 38 import org.springframework.aop.target.SingletonTargetSource; 39 import org.springframework.beans.BeansException; 40 import org.springframework.beans.PropertyValues; 41 import org.springframework.beans.factory.BeanClassLoaderAware; 42 import org.springframework.beans.factory.BeanFactory; 43 import org.springframework.beans.factory.BeanFactoryAware; 44 import org.springframework.beans.factory.config.ConfigurableBeanFactory; 45 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 46 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; 47 import org.springframework.core.Ordered; 48 import org.springframework.util.ClassUtils; 49 50 90 public abstract class AbstractAutoProxyCreator extends ProxyConfig 91 implements InstantiationAwareBeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, 92 Ordered, AopInfrastructureBean { 93 94 98 protected static final Object [] DO_NOT_PROXY = null; 99 100 105 protected static final Object [] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS = new Object [0]; 106 107 108 109 protected final Log logger = LogFactory.getLog(getClass()); 110 111 112 private int order = Integer.MAX_VALUE; 113 114 115 private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); 116 117 121 private boolean freezeProxy = false; 122 123 124 private String [] interceptorNames = new String [0]; 125 126 private boolean applyCommonInterceptorsFirst = true; 127 128 private TargetSourceCreator[] customTargetSourceCreators; 129 130 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 131 132 private BeanFactory beanFactory; 133 134 139 private final Set targetSourcedBeanNames = Collections.synchronizedSet(new HashSet ()); 140 141 142 148 public final void setOrder(int order) { 149 this.order = order; 150 } 151 152 public final int getOrder() { 153 return this.order; 154 } 155 156 162 public void setFrozen(boolean frozen) { 163 this.freezeProxy = frozen; 164 } 165 166 public boolean isFrozen() { 167 return this.freezeProxy; 168 } 169 170 175 public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) { 176 this.advisorAdapterRegistry = advisorAdapterRegistry; 177 } 178 179 193 public void setCustomTargetSourceCreators(TargetSourceCreator[] targetSourceCreators) { 194 this.customTargetSourceCreators = targetSourceCreators; 195 } 196 197 204 public void setInterceptorNames(String [] interceptorNames) { 205 this.interceptorNames = interceptorNames; 206 } 207 208 212 public void setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst) { 213 this.applyCommonInterceptorsFirst = applyCommonInterceptorsFirst; 214 } 215 216 public void setBeanClassLoader(ClassLoader classLoader) { 217 this.beanClassLoader = classLoader; 218 } 219 220 public void setBeanFactory(BeanFactory beanFactory) { 221 this.beanFactory = beanFactory; 222 } 223 224 228 protected BeanFactory getBeanFactory() { 229 return this.beanFactory; 230 } 231 232 233 public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { 234 if (isInfrastructureClass(beanClass, beanName) || shouldSkip(beanClass, beanName)) { 235 return null; 236 } 237 238 TargetSource targetSource = getCustomTargetSource(beanClass, beanName); 242 if (targetSource != null) { 243 this.targetSourcedBeanNames.add(beanName); 244 Object [] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource); 245 return createProxy(beanClass, beanName, specificInterceptors, targetSource); 246 } 247 248 return null; 249 } 250 251 public boolean postProcessAfterInstantiation(Object bean, String beanName) { 252 return true; 253 } 254 255 public PropertyValues postProcessPropertyValues( 256 PropertyValues pvs, PropertyDescriptor [] pds, Object bean, String beanName) { 257 258 return pvs; 259 } 260 261 public Object postProcessBeforeInitialization(Object bean, String beanName) { 262 return bean; 263 } 264 265 270 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 271 if (this.targetSourcedBeanNames.contains(beanName) || 272 isInfrastructureClass(bean.getClass(), beanName) || shouldSkip(bean.getClass(), beanName)) { 273 return bean; 274 } 275 276 Object [] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); 278 if (specificInterceptors != DO_NOT_PROXY) { 279 return createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); 280 } 281 282 return bean; 283 } 284 285 286 292 protected boolean isInfrastructureClass(Class beanClass, String beanName) { 293 return isInfrastructureClass(beanClass); 294 } 295 296 307 protected boolean isInfrastructureClass(Class beanClass) { 308 boolean retVal = Advisor.class.isAssignableFrom(beanClass) || 309 Advice.class.isAssignableFrom(beanClass) || 310 AopInfrastructureBean.class.isAssignableFrom(beanClass); 311 if (retVal && logger.isTraceEnabled()) { 312 logger.trace("Did not attempt to auto-proxy infrastructure class [" + beanClass.getName() + "]"); 313 } 314 return retVal; 315 } 316 317 325 protected boolean shouldSkip(Class beanClass, String beanName) { 326 return false; 327 } 328 329 339 protected TargetSource getCustomTargetSource(Class beanClass, String beanName) { 340 if (this.beanFactory != null && this.beanFactory.containsBean(beanName)) { 342 if (logger.isTraceEnabled()) { 343 logger.trace("Checking for custom TargetSource for bean with name '" + beanName + "'"); 344 } 345 if (this.customTargetSourceCreators != null) { 346 for (int i = 0; i < this.customTargetSourceCreators.length; i++) { 347 TargetSourceCreator tsc = this.customTargetSourceCreators[i]; 348 TargetSource ts = tsc.getTargetSource(beanClass, beanName); 349 if (ts != null) { 350 if (logger.isDebugEnabled()) { 352 logger.debug("TargetSourceCreator [" + tsc + 353 " found custom TargetSource for bean with name '" + beanName + "'"); 354 } 355 return ts; 356 } 357 } 358 } 359 } 360 361 return null; 363 } 364 365 376 protected Object createProxy( 377 Class beanClass, String beanName, Object [] specificInterceptors, TargetSource targetSource) { 378 379 ProxyFactory proxyFactory = new ProxyFactory(); 380 proxyFactory.copyFrom(this); 382 383 if (!shouldProxyTargetClass(beanClass, beanName)) { 384 Class [] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass); 387 for (int i = 0; i < targetInterfaces.length; i++) { 388 proxyFactory.addInterface(targetInterfaces[i]); 389 } 390 } 391 392 Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); 393 for (int i = 0; i < advisors.length; i++) { 394 proxyFactory.addAdvisor(advisors[i]); 395 } 396 397 proxyFactory.setTargetSource(targetSource); 398 customizeProxyFactory(proxyFactory); 399 400 proxyFactory.setFrozen(this.freezeProxy); 401 return proxyFactory.getProxy(this.beanClassLoader); 402 } 403 404 415 protected boolean shouldProxyTargetClass(Class beanClass, String beanName) { 416 return (isProxyTargetClass() || 417 (this.beanFactory instanceof ConfigurableListableBeanFactory && 418 AutoProxyUtils.shouldProxyTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName))); 419 } 420 421 429 protected Advisor[] buildAdvisors(String beanName, Object [] specificInterceptors) { 430 Advisor[] commonInterceptors = resolveInterceptorNames(); 432 433 List allInterceptors = new ArrayList (); 434 if (specificInterceptors != null) { 435 allInterceptors.addAll(Arrays.asList(specificInterceptors)); 436 if (commonInterceptors != null) { 437 if (this.applyCommonInterceptorsFirst) { 438 allInterceptors.addAll(0, Arrays.asList(commonInterceptors)); 439 } 440 else { 441 allInterceptors.addAll(Arrays.asList(commonInterceptors)); 442 } 443 } 444 } 445 if (logger.isDebugEnabled()) { 446 int nrOfCommonInterceptors = (commonInterceptors != null ? commonInterceptors.length : 0); 447 int nrOfSpecificInterceptors = (specificInterceptors != null ? specificInterceptors.length : 0); 448 logger.debug("Creating implicit proxy for bean '" + beanName + "' with " + nrOfCommonInterceptors + 449 " common interceptors and " + nrOfSpecificInterceptors + " specific interceptors"); 450 } 451 452 Advisor[] advisors = new Advisor[allInterceptors.size()]; 453 for (int i = 0; i < allInterceptors.size(); i++) { 454 advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i)); 455 } 456 return advisors; 457 } 458 459 463 private Advisor[] resolveInterceptorNames() { 464 ConfigurableBeanFactory cbf = 465 (this.beanFactory instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) this.beanFactory : null); 466 List advisors = new ArrayList (); 467 for (int i = 0; i < this.interceptorNames.length; i++) { 468 String beanName = this.interceptorNames[i]; 469 if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) { 470 Object next = this.beanFactory.getBean(beanName); 471 advisors.add(this.advisorAdapterRegistry.wrap(next)); 472 } 473 } 474 return (Advisor[]) advisors.toArray(new Advisor[advisors.size()]); 475 } 476 477 485 protected void customizeProxyFactory(ProxyFactory proxyFactory) { 486 } 487 488 489 505 protected abstract Object [] getAdvicesAndAdvisorsForBean( 506 Class beanClass, String beanName, TargetSource customTargetSource) throws BeansException; 507 508 } 509 | Popular Tags |