@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (!this.earlyProxyReferences.contains(cacheKey)) { //如果适合被代理,则需要封装指定bean return wrapIfNecessary(bean, beanName, cacheKey); } } return bean; }
@Override protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource targetSource) { //寻找有资格的、合适的增强 List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName); if (advisors.isEmpty()) { //无法找到对应的增强,返回null return DO_NOT_PROXY; } return advisors.toArray(); }
对于指定的bean的增强方法的获取,一定是包含两个步骤的,获取所有的增强,以及寻找所有增强中适用于bean的增强并应用。
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) { List<Advisor> candidateAdvisors = findCandidateAdvisors(); List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName); extendAdvisors(eligibleAdvisors); if (!eligibleAdvisors.isEmpty()) { eligibleAdvisors = sortAdvisors(eligibleAdvisors); } return eligibleAdvisors; }
@Override //candidate 候选人 protected List<Advisor> findCandidateAdvisors() { // Add all the Spring advisors found according to superclass rules. //调用父类方法,加载aop文件中的父类声明 List<Advisor> advisors = super.findCandidateAdvisors(); // Build Advisors for all AspectJ aspects in the bean factory. //获取bean的注解增强 advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors()); return advisors; }
如何获取bean的注解增强?
(1)、获取所有的beanName
(2)、遍历所有的beanName,并找出声明AspectJ注解的类
(3)、对标记为AspectJ注解的类进行增强器的提取
(4)、将提取结果加入缓存
//获取所有额beanName
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.beanFactory, Object.class, true, false); for (String beanName : beanNames) { //循环所有的beanName找出对应的增强方法 if (!isEligibleBean(beanName)) { //不合法额bean略过 continue; } // We must be careful not to instantiate beans eagerly as in this case they // would be cached by the Spring container but would not have been weaved. Class<?> beanType = this.beanFactory.getType(beanName); //找出对应的bean的类型 if (beanType == null) { continue; } if (this.advisorFactory.isAspect(beanType)) {//如果存在Aspect注解 aspectNames.add(beanName); AspectMetadata amd = new AspectMetadata(beanType, beanName); if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); //解析标记AspectJ注解中的增强方法 if (this.beanFactory.isSingleton(beanName)) { this.advisorsCache.put(beanName, classAdvisors); } else { this.aspectFactoryCache.put(beanName, factory); } advisors.addAll(classAdvisors); } else { // Per target or per this. if (this.beanFactory.isSingleton(beanName)) { throw new IllegalArgumentException("Bean with name '" + beanName + "' is a singleton, but aspect instantiation model is not singleton"); } MetadataAwareAspectInstanceFactory factory = new PrototypeAspectInstanceFactory(this.beanFactory, beanName); this.aspectFactoryCache.put(beanName, factory); advisors.addAll(this.advisorFactory.getAdvisors(factory)); } } }
//挑取出合适的增强器,也就是满足配置的通配符的增强器
protected List<Advisor> findAdvisorsThatCanApply( List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) { ProxyCreationContext.setCurrentProxiedBeanName(beanName); try { return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);//过滤已经得到的advisors } finally { ProxyCreationContext.setCurrentProxiedBeanName(null); } }