概述
APC(Auto Proxy Creator)抽象基类。从容器中仅获取Spring Advisor bean组件作为所要使用的AOP Advice来源,而不使用@AspectJ注解定义的AOP Advice bean组件作为来源。
继承自AbstractAutoProxyCreator,检测bean是否符合自动代理的条件,以及创建相应的自动代理的逻辑由此基类实现。
AbstractAdvisorAutoProxyCreator自身实现了基类定义的抽象方法#getAdvicesAndAdvisorsForBean()用于从容器中获取针对某个bean可用的AOP Advice和Spring Advisor bean。并且AbstractAdvisorAutoProxyCreator对#getAdvicesAndAdvisorsForBean()的实现只使用容器中的Spring Advisor,而不考虑AOP Advice。
AbstractAdvisorAutoProxyCreator约定一个bean需要被自动代理的条件是:容器中存在至少一个跟该bean匹配的Spring Advisors。这里一个bean跟容器中某个Spring Advisor bean是否匹配的匹配逻辑如下 :
AbstractAdvisorAutoProxyCreator自身提供一个方法boolean isEligibleAdvisorBean(String beanName)来根据Advisor bean名称声明该Advisor是否符合条件(此方法的缺省实现为总是返回true,子类可以覆盖实现该方法),当前APC工作时会首先从容其中筛选出所有符合该条件的Spring Advisors作为候选;- 从上面的
Spring Advisors候选中,利用bean类/方法上的注解信息再次筛选,选出那些跟当前bean匹配的Spring Advisors作为最终要包裹到当前bean的Spring Advisors。
AbstractAdvisorAutoProxyCreator虽然自身被定义为抽象,但它却没有定义抽象方法,只是从设计上要求具体实现子类要覆盖方法#findCandidateAdvisors()的缺省实现以满足自身的特定需求,比如仅仅保留某些符合条件的Spring Advisor等等。
有顺序要求的Spring Advisor可以实现接口org.springframework.core.Ordered接口,因为AbstractAdvisorAutoProxyCreator会对符合条件的Spring Advisor做相应的排序然后才包裹到目标bean上。
源代码
源代码版本 : spring-aop-5.1.5.RELEASE
package org.springframework.aop.framework.autoproxy;
import java.util.List;
import org.springframework.aop.Advisor;
import org.springframework.aop.TargetSource;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Generic auto proxy creator that builds AOP proxies for specific beans
* based on detected Advisors for each bean.
*
*
* Advisors or advices requiring ordering should implement the
* org.springframework.core.Ordered interface. This class sorts
* Advisors by Ordered order value. Advisors that don't implement the
* Ordered interface will be considered as unordered; they will appear
* at the end of the advisor chain in undefined order.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see #findCandidateAdvisors
*/
@SuppressWarnings("serial")
public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator {
@Nullable
private BeanFactoryAdvisorRetrievalHelper advisorRetrievalHelper;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException(
"AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: "
+ beanFactory);
}
initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
}
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 初始化从容器中获取符合条件的 Spring Advisor 的小助手
this.advisorRetrievalHelper = new BeanFactoryAdvisorRetrievalHelperAdapter(beanFactory);
}
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(
Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {
// 获取容器中所有针对 beanClass,beanName 可用的 Spring Advisors
List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
if (advisors.isEmpty()) {
// 如果一个可用的 Spring Advisor 都没有找到则返回 null,表示不要给当前bean创建代理
return DO_NOT_PROXY;
}
return advisors.toArray();
}
/**
* Find all eligible Advisors for auto-proxying this class.
* @param beanClass the clazz to find advisors for
* @param beanName the name of the currently proxied bean
* @return the empty List, not null,
* if there are no pointcuts or interceptors
* @see #findCandidateAdvisors
* @see #sortAdvisors
* @see #extendAdvisors
*/
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
// 从容器中找到所有的 Spring Advisors bean 组件作为候选,此结果尚未针对当前 bean 进行筛选
// 当前类中方法 #findCandidateAdvisors() 是一个抽象方法,要求由子类提供实现
List<Advisor> candidateAdvisors = findCandidateAdvisors();
// 将每个候选 Spring Advisor 针对当前 bean 进行筛选,留下符合条件的
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors,
beanClass, beanName);
// 对符合条件的 Spring Advisors 列表做扩展,当前类的该实现方法为空实现,
// 子实现类可以重写该方法做相应的扩展
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty()) {
// 排序
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
}
return eligibleAdvisors;
}
/**
* Find all candidate Advisors to use in auto-proxying.
* @return the List of candidate Advisors
*/
protected List<Advisor> findCandidateAdvisors() {
Assert.state(this.advisorRetrievalHelper != null,
"No BeanFactoryAdvisorRetrievalHelper available");
return this.advisorRetrievalHelper.findAdvisorBeans();
}
/**
* Search the given candidate Advisors to find all Advisors that
* can apply to the specified bean.
* @param candidateAdvisors the candidate Advisors
* @param beanClass the target's bean class
* @param beanName the target's bean name
* @return the List of applicable Advisors
* @see ProxyCreationContext#getCurrentProxiedBeanName()
*/
protected List<Advisor> findAdvisorsThatCanApply(
List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {
ProxyCreationContext.setCurrentProxiedBeanName(beanName);
try {
return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
}
finally {
ProxyCreationContext.setCurrentProxiedBeanName(null);
}
}
/**
* Return whether the Advisor bean with the given name is eligible
* for proxying in the first place.
* @param beanName the name of the Advisor bean
* @return whether the bean is eligible
*/
protected boolean isEligibleAdvisorBean(String beanName) {
return true;
}
/**
* Sort advisors based on ordering. Subclasses may choose to override this
* method to customize the sorting strategy.
* @param advisors the source List of Advisors
* @return the sorted List of Advisors
* @see org.springframework.core.Ordered
* @see org.springframework.core.annotation.Order
* @see org.springframework.core.annotation.AnnotationAwareOrderComparator
*/
protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
AnnotationAwareOrderComparator.sort(advisors);
return advisors;
}
/**
* Extension hook that subclasses can override to register additional Advisors,
* given the sorted Advisors obtained to date.
* The default implementation is empty.
* Typically used to add Advisors that expose contextual information
* required by some of the later advisors.
* @param candidateAdvisors the Advisors that have already been identified as
* applying to a given bean
*/
protected void extendAdvisors(List<Advisor> candidateAdvisors) {
}
/**
* This auto-proxy creator always returns pre-filtered Advisors.
*/
@Override
protected boolean advisorsPreFiltered() {
return true;
}
/**
* Subclass of BeanFactoryAdvisorRetrievalHelper that delegates to
* surrounding AbstractAdvisorAutoProxyCreator facilities.
*/
private class BeanFactoryAdvisorRetrievalHelperAdapter
extends BeanFactoryAdvisorRetrievalHelper {
public BeanFactoryAdvisorRetrievalHelperAdapter(
ConfigurableListableBeanFactory beanFactory) {
super(beanFactory);
}
@Override
protected boolean isEligibleBean(String beanName) {
return AbstractAdvisorAutoProxyCreator.this.isEligibleAdvisorBean(beanName);
}
}
}

1075

被折叠的 条评论
为什么被折叠?



