1 前言
我们了解过AOP配置解析以及AOP的切入时机,那么这篇我们看看,AOP是如何给某个Bean筛选合适的通知器。
2 源码分析
2.1 方法通读
我们从AOP切入时机中,为当前bean筛选通知器开始继续看起:
protected Object[] getAdvicesAndAdvisorsForBean( Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) { // 为当前的bean筛选合适的通知器 List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName); // 如果集合为空表示不需要代理 if (advisors.isEmpty()) { return DO_NOT_PROXY; } return advisors.toArray(); } protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) { // 获取所有的通知器 List<Advisor> candidateAdvisors = findCandidateAdvisors(); // 筛选哪些是可以作用到当前bean的 // 通过 ClassFilter 和 MethodMatcher 对目标类和方法进行匹配 List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName); // 扩展操作 extendAdvisors(eligibleAdvisors); if (!eligibleAdvisors.isEmpty()) { // 通知器排序根据order eligibleAdvisors = sortAdvisors(eligibleAdvisors); } return eligibleAdvisors; }
2.2 findAdvisorsThatCanApply
(1)派小弟AopUtils来筛选,我们继续往里走
protected List<Advisor> findAdvisorsThatCanApply( List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) { ProxyCreationContext.setCurrentProxiedBeanName(beanName); try { // 派AopUtils来进行筛选 return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass); } finally { ProxyCreationContext.setCurrentProxiedBeanName(null); } }
(2)
标签:eligibleAdvisors,Spring,beanName,List,源码,AOP,筛选,beanClass From: https://www.cnblogs.com/kukuxjx/p/17136109.html