001/* 002 * Copyright 2002-2015 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.aop.framework.autoproxy; 018 019import org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor; 020import org.springframework.aop.framework.ProxyFactory; 021import org.springframework.beans.factory.BeanFactory; 022import org.springframework.beans.factory.BeanFactoryAware; 023import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 024 025/** 026 * Extension of {@link AbstractAutoProxyCreator} which implements {@link BeanFactoryAware}, 027 * adds exposure of the original target class for each proxied bean 028 * ({@link AutoProxyUtils#ORIGINAL_TARGET_CLASS_ATTRIBUTE}), 029 * and participates in an externally enforced target-class mode for any given bean 030 * ({@link AutoProxyUtils#PRESERVE_TARGET_CLASS_ATTRIBUTE}). 031 * This post-processor is therefore aligned with {@link AbstractAutoProxyCreator}. 032 * 033 * @author Juergen Hoeller 034 * @since 4.2.3 035 * @see AutoProxyUtils#shouldProxyTargetClass 036 * @see AutoProxyUtils#determineTargetClass 037 */ 038@SuppressWarnings("serial") 039public abstract class AbstractBeanFactoryAwareAdvisingPostProcessor extends AbstractAdvisingBeanPostProcessor 040 implements BeanFactoryAware { 041 042 private ConfigurableListableBeanFactory beanFactory; 043 044 045 @Override 046 public void setBeanFactory(BeanFactory beanFactory) { 047 this.beanFactory = (beanFactory instanceof ConfigurableListableBeanFactory ? 048 (ConfigurableListableBeanFactory) beanFactory : null); 049 } 050 051 @Override 052 protected ProxyFactory prepareProxyFactory(Object bean, String beanName) { 053 if (this.beanFactory != null) { 054 AutoProxyUtils.exposeTargetClass(this.beanFactory, beanName, bean.getClass()); 055 } 056 057 ProxyFactory proxyFactory = super.prepareProxyFactory(bean, beanName); 058 if (!proxyFactory.isProxyTargetClass() && this.beanFactory != null && 059 AutoProxyUtils.shouldProxyTargetClass(this.beanFactory, beanName)) { 060 proxyFactory.setProxyTargetClass(true); 061 } 062 return proxyFactory; 063 } 064 065}