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