spring源码分析之AOP开启注解

AOP开启注解

在使用注解@Aspect来进行AOP操作时,需要在xml中进行配置

xml 复制代码
<!-- 使@Aspect注解生效 -->
<aop:aspectj-autoproxy/>

创建BeanFactory时obtainFreshBeanFactory()在解析xml加载BeanDefinition中,执行parseBeanDefinitions方法进行解析发现其内有逻辑是

java 复制代码
delegate.parseCustomElement(ele)

即进行自定义标签的解析,会去META-INF/spring.handlers中寻找对应的handler,该标签的namespace是www.springframework.org/schema/aop,去spring.handlers中找到对应的记录

java 复制代码
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
properties 复制代码
http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler

还有一个配置是spring.schemas,以找到对应的xsd文件

properties 复制代码
http\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd

然后执行该handler中的init方法

java 复制代码
NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
namespaceHandler.init();

即执行AopNamespaceHandler#init

AopNamespaceHandler#init

java 复制代码
public void init() {
   // In 2.0 XSD as well as in 2.5+ XSDs
   registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
   registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
   registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());

   // Only in 2.0 XSD: moved to context namespace in 2.5+
   registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
}

这里可以看到aspectj-autoproxy,也就是说在配置文件中如果存在<aop:aspectj-autoproxy/>就会使用AspectJAutoProxyBeanDefinitionParser进行解析,调用parse方法

java 复制代码
class AspectJAutoProxyBeanDefinitionParser implements BeanDefinitionParser {

   @Override
   public BeanDefinition parse(Element element, ParserContext parserContext) {
     // 注册AnnotationAwareAspectJAutoProxyCreator
     // AUTO_PROXY_CREATOR_BEAN_NAME ="org.springframework.aop.config.internalAutoProxyCreator"
     // 如果标签中有"proxy-target-class"属性会进行下一步的解析
      AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
     // 对于注解中子节点的处理
      extendBeanDefinition(element, parserContext);
      return null;
   }

   private void extendBeanDefinition(Element element, ParserContext parserContext) {
      BeanDefinition beanDef =
            parserContext.getRegistry().getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
      if (element.hasChildNodes()) {
         addIncludePatterns(element, parserContext, beanDef);
      }
   }

   private void addIncludePatterns(Element element, ParserContext parserContext, BeanDefinition beanDef) {
      ManagedList<TypedStringValue> includePatterns = new ManagedList<TypedStringValue>();
      NodeList childNodes = element.getChildNodes();
      for (int i = 0; i < childNodes.getLength(); i++) {
         Node node = childNodes.item(i);
         if (node instanceof Element) {
            Element includeElement = (Element) node;
            TypedStringValue valueHolder = new TypedStringValue(includeElement.getAttribute("name"));
            valueHolder.setSource(parserContext.extractSource(includeElement));
            includePatterns.add(valueHolder);
         }
      }
      if (!includePatterns.isEmpty()) {
         includePatterns.setSource(parserContext.extractSource(element));
         beanDef.getPropertyValues().add("includePatterns", includePatterns);
      }
   }

}

而在registerBeanPostProcessors(beanFactory)中会去进行注册BeanPostProcessor,在上面解析xml注册的AnnotationAwareAspectJAutoProxyCreator的结构还挺复杂,实现的重要的两个接口都标出来了,实现了BeanPostProcessor和BeanFactoryAware两个接口

在该registerBeanPostProcessors(beanFactory)方法中会找到beanFactory中所有BeanPostProcessor的类,然后获取到该类实例

java 复制代码
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// 注册AnnotationAwareAspectJAutoProxyCreator时的beanName为org.springframework.aop.config.internalAutoProxyCreator
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);

在获取bean的时候会创建bean,但是不要忘了该类还实现了一个BeanFactoryAware接口,在创建bean之后会进行初始化,也就会执行org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeAwareMethods方法

java 复制代码
if (bean instanceof BeanFactoryAware) {
   ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}

这样就会执行AbstractAdvisorAutoProxyCreator#setBeanFactory,也没干什么事,就是给几个变量赋了个值

java 复制代码
// org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#setBeanFactory
@Override
public void setBeanFactory(BeanFactory beanFactory) {
   super.setBeanFactory(beanFactory);
   if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
      throw new IllegalArgumentException(
            "AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: " + beanFactory);
   }
   initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
}

// org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator#initBeanFactory
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  // advisorRetrievalHelper赋值 BeanFactoryAdvisorRetrievalHelperAdapter实例化
		super.initBeanFactory(beanFactory);
		if (this.aspectJAdvisorFactory == null) {
			this.aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(beanFactory);
		}
		this.aspectJAdvisorsBuilder =
				new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);
	}

进行处理

由于实现了BeanPostProcessor接口,在进行实例化bean的前后会调用该类的postProcessBeforeInitialization和postProcessAfterInitialization

这里AnnotationAwareAspectJAutoProxyCreator是继承的AbstractAutoProxyCreator类

java 复制代码
// # AbstractAutoProxyCreator
@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		Object cacheKey = getCacheKey(beanClass, beanName);

		if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
			if (this.advisedBeans.containsKey(cacheKey)) {
				return null;
			}
      // shouldSkip获取切面信息加入到缓存中
			if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
				this.advisedBeans.put(cacheKey, Boolean.FALSE);
				return null;
			}
		}

		// Create proxy here if we have a custom TargetSource.
		// Suppresses unnecessary default instantiation of the target bean:
		// The TargetSource will handle target instances in a custom fashion.
		if (beanName != null) {
			TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
			if (targetSource != null) {
				this.targetSourcedBeans.add(beanName);
				Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
				Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
				this.proxyTypes.put(cacheKey, proxy.getClass());
				return proxy;
			}
		}

		return null;
	}


@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (bean != null) {
      // 根据给定的bean的class和name生成一个key
			Object cacheKey = getCacheKey(bean.getClass(), beanName);
			if (this.earlyProxyReferences.remove(cacheKey) != bean) {
        // 进行代理
				return wrapIfNecessary(bean, beanName, cacheKey);
			}
		}
		return bean;
	}

在创建bean的过程中调用了org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation来进行调用BeanPostProcessor接口

java 复制代码
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
   Object bean = null;
   if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
      // Make sure bean class is actually resolved at this point.
      if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
         Class<?> targetType = determineTargetType(beanName, mbd);
         if (targetType != null) {
           // 前置处理 调用BeanPostProcessor#postProcessBeforeInstantiation
            bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
            if (bean != null) {
              // 后置处理 调用BeanPostProcessor#postProcessAfterInitialization
               bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
            }
         }
      }
      mbd.beforeInstantiationResolved = (bean != null);
   }
   return bean;
}

wrapIfNecessary

java 复制代码
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
  // 已经处理过则无需处理
   if (beanName != null && this.targetSourcedBeans.contains(beanName)) {
      return bean;
   }
  // 无需进行增强
   if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
      return bean;
   }
  // 如果是继承自Advice、Pointcut、Advisor、AopInfrastructureBean的类,或者@Aspect注解标注的类不需要代理
   if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
      this.advisedBeans.put(cacheKey, Boolean.FALSE);
      return bean;
   }

   // Create proxy if we have advice.
  // 如果存在增强方法则创建代理
   Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
   if (specificInterceptors != DO_NOT_PROXY) {
      this.advisedBeans.put(cacheKey, Boolean.TRUE);
     // 创建代理
      Object proxy = createProxy(
            bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
      this.proxyTypes.put(cacheKey, proxy.getClass());
      return proxy;
   }

   this.advisedBeans.put(cacheKey, Boolean.FALSE);
   return bean;
}
getAdvicesAndAdvisorsForBean查找增强器
java 复制代码
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource targetSource) {
   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();
  // 寻找匹配的增强器
		List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
		extendAdvisors(eligibleAdvisors);
		if (!eligibleAdvisors.isEmpty()) {
			eligibleAdvisors = sortAdvisors(eligibleAdvisors);
		}
		return eligibleAdvisors;
	}

// org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator#findCandidateAdvisors
protected List<Advisor> findCandidateAdvisors() {
		// Add all the Spring advisors found according to superclass rules.
  // 这里处理的是xml方式的
		List<Advisor> advisors = super.findCandidateAdvisors();
		// Build Advisors for all AspectJ aspects in the bean factory.
  // 解析@Aspect注解中的增强方法,进行解析
		advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
		return advisors;
	}
createProxy创建代理
java 复制代码
protected Object createProxy(
      Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {

   if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
      AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
   }

   ProxyFactory proxyFactory = new ProxyFactory();
   proxyFactory.copyFrom(this);
// 是接口需要代理还是类需要代理
   if (!proxyFactory.isProxyTargetClass()) {
      if (shouldProxyTargetClass(beanClass, beanName)) {
         proxyFactory.setProxyTargetClass(true);
      }
      else {
         evaluateProxyInterfaces(beanClass, proxyFactory);
      }
   }

   Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
   proxyFactory.addAdvisors(advisors);
  // 设置需要代理的类
   proxyFactory.setTargetSource(targetSource);
  // 定制代理
   customizeProxyFactory(proxyFactory);

   proxyFactory.setFrozen(this.freezeProxy);
   if (advisorsPreFiltered()) {
      proxyFactory.setPreFiltered(true);
   }

   return proxyFactory.getProxy(getProxyClassLoader());
}

突然发现就创建个代理对象,spring考虑的是方方面面的,太强大了

何处调用的呢?

既然知道了AbstractAutoProxyCreator是BeanPostProcessor接口,那么就找到spring是在哪里调用的BeanPostProcessor。

是在createBean环节中的initializeBean初始化来进行调用的

java 复制代码
Object exposedObject = bean;
try {
   populateBean(beanName, mbd, instanceWrapper);
   if (exposedObject != null) {
      exposedObject = initializeBean(beanName, exposedObject, mbd);
   }
}

来看一下initializeBean初始化中调用了哪些

java 复制代码
// 执行的是Aware接口的对应方法 ((BeanNameAware) bean).setBeanName(beanName)、((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader())、((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this)
invokeAwareMethods(beanName, bean);
// 调用的是BeanPostProcessor的postProcessBeforeInitialization方法
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
// 调用的是((InitializingBean) bean).afterPropertiesSet()以及自定义的init方法
invokeInitMethods(beanName, wrappedBean, mbd);
// 调用BeanPostProcessor的postProcessAfterInitialization方法
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

可以看到有两个地方分别调用的BeanPostProcessor的postProcessBeforeInitialization方法、postProcessAfterInitialization方法,找到源头了

注意:

如果存在循环依赖的话,会在调用三级缓存中的ObjectFactory.getObject()进行aop代理,使得得到的早期对象是代理对象

java 复制代码
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
   Object exposedObject = bean;
   if (bean != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
      for (BeanPostProcessor bp : getBeanPostProcessors()) {
         if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
            SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
           // 这里做了个代理,也就是AOP  (AbstractAutoProxyCreator继承的SmartInstantiationAwareBeanPostProcessor,也就是在getEarlyBeanReference方法中调用的wrapIfNecessary方法)
            exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
            if (exposedObject == null) {
               return null;
            }
         }
      }
   }
   return exposedObject;
}

zhhll.icu/2022/框架/spr...

本文由mdnice多平台发布

相关推荐
初晴~2 小时前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
雷神乐乐4 小时前
Spring学习(一)——Sping-XML
java·学习·spring
小林coding4 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云
文大。5 小时前
2024年广西职工职业技能大赛-Spring
java·spring·网络安全
小马爱打代码6 小时前
Spring Boot 中 Map 的最佳实践
java·spring boot·spring
智慧老师11 小时前
Spring基础分析13-Spring Security框架
java·后端·spring
hanbarger14 小时前
mybatis框架——缓存,分页
java·spring·mybatis
龙少954317 小时前
【深入理解@EnableCaching】
java·后端·spring
啦啦右一1 天前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
荆州克莱1 天前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术