博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring5.1.x源码解析之五(AOP)
阅读量:633 次
发布时间:2019-03-13

本文共 41667 字,大约阅读时间需要 138 分钟。

文章目录

编写bean

@AllArgsConstructor@Data@NoArgsConstructorpublic class MyBeanTest implements InitializingBean, DisposableBean {
private String testStr = "testStr"; public void test() {
System.out.println(testStr); } @Override public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean"); } @Override public void destroy() throws Exception {
System.out.println("死了"); }}

编写需要Advisor

// 指定当前类为切面类@Aspectpublic class AopTest {
/** * 指定切入点表达式,拦截那些方法,那些方法就生成代理对象。 */ @Pointcut("execution (* *.test(..))") public void test() {
} //前置通知:执行目标方法之前执行 @Before("test()") public void begin() {
System.out.println("开启事务"); } //后置通知:执行目标方法之后执行,是否出现异常都会执行。 @After("test()") public void commit() {
System.out.println("提交事务"); } // 异常通知:目标方法执行出现异常,执行此关注代码 @AfterThrowing("test()") public void afterThrowing() {
System.out.println("afterThrowing"); } //返回后通知:在调用目标方法结束后执行,出现异常不执行。 @AfterReturning("test()") public void afterReturning() {
System.out.println("AfterReturning"); } //环绕通知:环绕目标方法执行 @Around("test()") public void around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("环绕前"); pj.proceed();//执行目标方法 System.out.println("环绕后"); }}

配置文件

执行流程,主要是根据spring-aop/src/main/resources/META-INF/spring.handlers注入的配置命名空间处理器

http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler

可以看到aspectj-autoproxy这个属性,正是配置文件声明AOP的命名空间.

public class AopNamespaceHandler extends NamespaceHandlerSupport {
/** * Register the {@link BeanDefinitionParser BeanDefinitionParsers} for the * '{@code config}', '{@code spring-configured}', '{@code aspectj-autoproxy}' * and '{@code scoped-proxy}' tags. */ @Override public void init() {
// In 2.0 XSD as well as in 2.1 XSD. registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser()); registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser()); registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator()); // Only in 2.0 XSD: moved to context namespace as of 2.1 registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser()); }}

查看他的接口,发现只有这一个方法,说明就是根据这个执行的解析命名空间

public interface BeanDefinitionParser {
@Nullable BeanDefinition parse(Element element, ParserContext parserContext);}

现在进入AspectJAutoProxyBeanDefinitionParser类解析parse

/*	注册AnnotationAwareAspectJAutoProxyCreator	对于注解中子类的处理	 */	public BeanDefinition parse(Element element, ParserContext parserContext) {
//注册AnnotationAwareAspectJAutoProxyCreator AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element); //对于注解中子类的处理 extendBeanDefinition(element, parserContext); return null; }
/*	注册或升级AutoProxyCreator定义beanName为internalAutoProxyCreator的BeanDefinition	对于proxy-target-class以及expose-proxy属性的处理	注册组件并通知,便于监听器做进一步处理	 */	public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(			ParserContext parserContext, Element sourceElement) {
//注册或升级AutoProxyCreator定义beanName为internalAutoProxyCreator的BeanDefinition BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary( parserContext.getRegistry(), parserContext.extractSource(sourceElement)); //对于proxy-target-class以及expose-proxy属性的处理 useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement); //注册组件并通知,便于监听器做进一步处理 registerComponentIfNecessary(beanDefinition, parserContext); }
/*	设置默认org.springframework.aop.config.AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME	 */	@Nullable	public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(			BeanDefinitionRegistry registry, @Nullable Object source) {
//aop的实现基本都是AnnotationAwareAspectJAutoProxyCreator完成的,他可以根据@Point定义的切点自动代理相匹配的bean return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source); }/* 创建AUTO_PROXY_CREATOR_BEAN_NAME */ private static BeanDefinition registerOrEscalateApcAsRequired( Class
cls, BeanDefinitionRegistry registry, @Nullable Object source) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); //如果已经存在了自动代理创建器且存在的创建器与现在的不一致那么需要根据优先级判断到底使用哪个 if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME); if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName()); int requiredPriority = findPriorityForClass(cls); //根据优先级判断使用哪个 if (currentPriority < requiredPriority) {
//改变bean所对应的className属性 apcDefinition.setBeanClassName(cls.getName()); } } //如果已经存在自动代理创建器并且与将要创建的一致,那么无需创建 return null; } //声明AnnotationAwareAspectJAutoProxyCreator注册为新的RootBeanDefinition RootBeanDefinition beanDefinition = new RootBeanDefinition(cls); beanDefinition.setSource(source); beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition); return beanDefinition; }
/*	对XML元素proxy-target-class和expose-proxy的处理	 */	private static void useClassProxyingIfNecessary(BeanDefinitionRegistry registry, @Nullable Element sourceElement) {
if (sourceElement != null) {
/* 使用jdk代理和cglib, 使用cglib需要注意 无法通知(advise)Final方法,因为不能被重写 需要将cglib二进制发行包放在classpath下面 强制使用cglib,需要设置true
*/ //配置是否有proxy-target-class属性 boolean proxyTargetClass = Boolean.parseBoolean(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE)); if (proxyTargetClass) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); } //有时候目标对象内部的自我调用无法实现切面中的增强 //当嵌套事务的时候,内部事务不会代理,如果想要子事务代理,则需要设置expose-proxy=true,同时在父事务使用AopContext.currentProxy()执行子事务 boolean exposeProxy = Boolean.parseBoolean(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE)); if (exposeProxy) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry); } } }
//	proxyTargetClass属性设置	public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) {
if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME); definition.getPropertyValues().add("proxyTargetClass", Boolean.TRUE); } } // exposeProxy属性设置 public static void forceAutoProxyCreatorToExposeProxy(BeanDefinitionRegistry registry) {
if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME); definition.getPropertyValues().add("exposeProxy", Boolean.TRUE); } }

至止加载命名空间解析器完成.

接下来来看看他的切点到底是什么时候执行的.首先看注册的默认AnnotationAwareAspectJAutoProxyCreator接口层次
在这里插入图片描述
可以发现主要实现了AwareBeanPostProcessor,Aware主要是资源的获取跟我们想要的无关.BeanPostProcessor在每次bean初始化的时候执行,我们看实现的方法

/*	构建key	封装对象	 */	public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
//构建key,工厂bean格式&beanName Object cacheKey = getCacheKey(bean.getClass(), beanName); //创建对象完成后赋值之前,利用工厂创建了bean,同时添加到了earlyProxyReferences进行代理,主要避免循环依赖 //主要看org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean //但该对象创建完成后,表明不在需要利用工厂创建对象,从而避免循环依赖的问题,于是该对象可以删除 //如果删除对象和bean一致,说明对象一样,不需要进行代理 if (this.earlyProxyReferences.remove(cacheKey) != bean) {
//封装指定bean return wrapIfNecessary(bean, beanName, cacheKey); } } return bean; }
/*		获取缓存bean		判断是否增强		判断是否基础设施类		获取增强方法并进行代理		加入缓存	 */	protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
//如果已经处理过 if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean; } //无需增强 if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean; } //给定的bean类是否代表一个基础设施类,基础设施类不应代理,或者配置了指定的bean不需要代理 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; } protected Object[] getAdvicesAndAdvisorsForBean( Class
beanClass, String beanName, @Nullable TargetSource targetSource) {
//寻找代理切面 List
advisors = findEligibleAdvisors(beanClass, beanName); if (advisors.isEmpty()) {
return DO_NOT_PROXY; } return advisors.toArray(); } /* 寻找匹配的增强器 并执行子类覆盖扩展 排序返回 */ protected List
findEligibleAdvisors(Class
beanClass, String beanName) {
//获取所有增强器 List
candidateAdvisors = findCandidateAdvisors(); //寻找匹配增强器 List
eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName); //执行扩展,子类可覆盖 extendAdvisors(eligibleAdvisors); if (!eligibleAdvisors.isEmpty()) {
//排序 eligibleAdvisors = sortAdvisors(eligibleAdvisors); } return eligibleAdvisors; }

获取所有增强器

/*	获取所有aop声明	 */	protected List
findCandidateAdvisors() {
// Add all the Spring advisors found according to superclass rules. //调用父类加载配置文件aop声明 List
advisors = super.findCandidateAdvisors(); // Build Advisors for all AspectJ aspects in the bean factory. if (this.aspectJAdvisorsBuilder != null) {
//获取容器所有切面aop声明 advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors()); } return advisors; }
/*	获取所有的beanName并遍历		找出beanName对应的calss是否存在Aspect注解			解析注解标记的增强方法	记录缓存并返回	 */	public List
buildAspectJAdvisors() {
List
aspectNames = this.aspectBeanNames; if (aspectNames == null) {
synchronized (this) {
aspectNames = this.aspectBeanNames; if (aspectNames == null) {
List
advisors = new ArrayList<>(); aspectNames = new ArrayList<>(); //获取所有beanName String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.beanFactory, Object.class, true, false); //循环找出所有beanName对应的增强方法 for (String beanName : beanNames) {
//不合法beanName跳过 if (!isEligibleBean(beanName)) {
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. //获取对应beanName的class类型 Class
beanType = this.beanFactory.getType(beanName); if (beanType == null) {
continue; } //如果存在Aspect注解,同时没有ajc$字段 if (this.advisorFactory.isAspect(beanType)) {
aspectNames.add(beanName); AspectMetadata amd = new AspectMetadata(beanType, beanName); if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
//分别封装bean的name和class MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); //解析注解标记的增强方法 List
classAdvisors = this.advisorFactory.getAdvisors(factory); 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)); } } } this.aspectBeanNames = aspectNames; return advisors; } } } if (aspectNames.isEmpty()) {
return Collections.emptyList(); } //记录缓存返回 List
advisors = new ArrayList<>(); for (String aspectName : aspectNames) {
List
cachedAdvisors = this.advisorsCache.get(aspectName); if (cachedAdvisors != null) { advisors.addAll(cachedAdvisors); } else { MetadataAwareAspectInstanceFactory factory = this.aspectFactoryCache.get(aspectName); advisors.addAll(this.advisorFactory.getAdvisors(factory)); } } return advisors; }
/*	验证	获取本类包括父类接口非PointCut注解所有公有方法		获取方法上注解符合预定于注解的增强器	同步实例化增强器	获取DeclareParents注解的方法增强器	 */	public List
getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
//获取标记为AspectJ的类 Class
aspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass(); //获取标记为AspectJ的name String aspectName = aspectInstanceFactory.getAspectMetadata().getAspectName(); //验证 validate(aspectClass); // We need to wrap the MetadataAwareAspectInstanceFactory with a decorator // so that it will only instantiate once. //装饰延迟初始化的AspectInstanceFactory MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory = new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory); List
advisors = new ArrayList<>(); //获取本类包括父类接口非PointCut注解所有公有方法 for (Method method : getAdvisorMethods(aspectClass)) {
//获取方法上注解符合预定于注解的增强器 Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName); if (advisor != null) {
advisors.add(advisor); } } // If it's a per target aspect, emit the dummy instantiating aspect. //同步实例化增强器 if (!advisors.isEmpty() && lazySingletonAspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
//如果寻找的增强器不为空,且有配置了增强延迟初始化那么需要在首位加入同步实例化增强器,保证增强之前对bean的实例化 Advisor instantiationAdvisor = new SyntheticInstantiationAdvisor(lazySingletonAspectInstanceFactory); advisors.add(0, instantiationAdvisor); } // Find introduction fields. //获取DeclareParents注解 //获取引介增强的注解形式实现 for (Field field : aspectClass.getDeclaredFields()) {
Advisor advisor = getDeclareParentsAdvisor(field); if (advisor != null) {
advisors.add(advisor); } } return advisors; }
/*	校验	切点信息获取	根据切点信息生成增强器	 */	public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory,			int declarationOrderInAspect, String aspectName) {
//校验 validate(aspectInstanceFactory.getAspectMetadata().getAspectClass()); //切点信息获取,主要封装了表达式和class,name AspectJExpressionPointcut expressionPointcut = getPointcut( candidateAdviceMethod, aspectInstanceFactory.getAspectMetadata().getAspectClass()); if (expressionPointcut == null) {
return null; } //根据切点信息生成增强器 return new InstantiationModelAwarePointcutAdvisorImpl(expressionPointcut, candidateAdviceMethod, this, aspectInstanceFactory, declarationOrderInAspect, aspectName); }
/*	获取匹配预定义注解对应方法上的注解	封装获取信息	封装提取得到的表达式以及工厂	 */	private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class
candidateAspectClass) {
//获取匹配预定义注解对应方法上的注解 AspectJAnnotation
aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod); if (aspectJAnnotation == null) {
return null; } //封装获取信息 AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(candidateAspectClass, new String[0], new Class
[0]); //提取得到的表达式,如:@around(“test()”)的test() ajexp.setExpression(aspectJAnnotation.getPointcutExpression()); if (this.beanFactory != null) {
ajexp.setBeanFactory(this.beanFactory); } return ajexp; }
/*	匹配预定义的方法注解	 */	protected static AspectJAnnotation
findAspectJAnnotationOnMethod(Method method) {
//ASPECTJ_ANNOTATION_CLASSES:需要匹配注解的类提前类静态加载了 for (Class
clazz : ASPECTJ_ANNOTATION_CLASSES) {
//获取指定方法上的注解 AspectJAnnotation
foundAnnotation = findAnnotation(method, (Class
) clazz); if (foundAnnotation != null) {
return foundAnnotation; } } return null; }
/*	这里主要封装一些信息	以及针对不同注解生成不同注解器解析	 */	public InstantiationModelAwarePointcutAdvisorImpl(AspectJExpressionPointcut declaredPointcut,			Method aspectJAdviceMethod, AspectJAdvisorFactory aspectJAdvisorFactory,			MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
this.declaredPointcut = declaredPointcut; this.declaringClass = aspectJAdviceMethod.getDeclaringClass(); this.methodName = aspectJAdviceMethod.getName(); this.parameterTypes = aspectJAdviceMethod.getParameterTypes(); this.aspectJAdviceMethod = aspectJAdviceMethod; this.aspectJAdvisorFactory = aspectJAdvisorFactory; this.aspectInstanceFactory = aspectInstanceFactory; this.declarationOrder = declarationOrder; this.aspectName = aspectName; if (aspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
// Static part of the pointcut is a lazy type. Pointcut preInstantiationPointcut = Pointcuts.union( aspectInstanceFactory.getAspectMetadata().getPerClausePointcut(), this.declaredPointcut); // Make it dynamic: must mutate from pre-instantiation to post-instantiation state. // If it's not a dynamic pointcut, it may be optimized out // by the Spring AOP infrastructure after the first evaluation. this.pointcut = new PerTargetInstantiationModelPointcut( this.declaredPointcut, preInstantiationPointcut, aspectInstanceFactory); this.lazy = true; } else {
// A singleton aspect. this.pointcut = this.declaredPointcut; this.lazy = false; //根据注解中的信息初始化对应的增强器,如@Before和@After分别初始化不同增强器 this.instantiatedAdvice = instantiateAdvice(this.declaredPointcut); } }
private Advice instantiateAdvice(AspectJExpressionPointcut pointcut) {
// 根据注解不同获取不同增强器 Advice advice = this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pointcut, this.aspectInstanceFactory, this.declarationOrder, this.aspectName); return (advice != null ? advice : EMPTY_ADVICE); }
/*	验证	封装注解类型以及切点表达式	判断是否类上有Aspect注解	根据注解不同封装不同增强器	配置Advice	 */	public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut,			MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
Class
candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass(); //校验 validate(candidateAspectClass); //封装注解类型以及切点表达式 AspectJAnnotation
aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod); if (aspectJAnnotation == null) {
return null; } // If we get here, we know we have an AspectJ method. // Check that it's an AspectJ-annotated class //判断是否类上有Aspect注解 if (!isAspect(candidateAspectClass)) {
throw new AopConfigException("Advice must be declared inside an aspect type: " + "Offending method '" + candidateAdviceMethod + "' in class [" + candidateAspectClass.getName() + "]"); } if (logger.isDebugEnabled()) {
logger.debug("Found AspectJ method: " + candidateAdviceMethod); } AbstractAspectJAdvice springAdvice; //根据注解不同封装不同增强器 switch (aspectJAnnotation.getAnnotationType()) {
case AtPointcut: if (logger.isDebugEnabled()) {
logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'"); } return null; case AtAround: springAdvice = new AspectJAroundAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); break; case AtBefore: //MethodBeforeAdviceInterceptor springAdvice = new AspectJMethodBeforeAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); break; case AtAfter: springAdvice = new AspectJAfterAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); break; case AtAfterReturning: springAdvice = new AspectJAfterReturningAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterReturningAnnotation.returning())) {
springAdvice.setReturningName(afterReturningAnnotation.returning()); } break; case AtAfterThrowing: springAdvice = new AspectJAfterThrowingAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
springAdvice.setThrowingName(afterThrowingAnnotation.throwing()); } break; default: throw new UnsupportedOperationException( "Unsupported advice type on method: " + candidateAdviceMethod); } // Now to configure the advice... springAdvice.setAspectName(aspectName); //添加到Advice集合的索引 springAdvice.setDeclarationOrder(declarationOrder); //获取方法注解配置的argNames String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod); if (argNames != null) {
springAdvice.setArgumentNamesFromStringArray(argNames); } springAdvice.calculateArgumentBindings(); return springAdvice; }
protected static class SyntheticInstantiationAdvisor extends DefaultPointcutAdvisor {
/* 同步实例化增强器初始化 */ public SyntheticInstantiationAdvisor(final MetadataAwareAspectInstanceFactory aif) {
super(aif.getAspectMetadata().getPerClausePointcut(), //目标方法前调用,类似@Before (MethodBeforeAdvice)(method, args, target) -> //简单初始化aspect aif.getAspectInstance()); } }

寻找匹配增强器

/*	过滤已经得到的advisors	 */	protected List
findAdvisorsThatCanApply( List
candidateAdvisors, Class
beanClass, String beanName) {
ProxyCreationContext.setCurrentProxiedBeanName(beanName);//设置当前代理的名称 try {
//过滤已经得到的advisors return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass); } finally {
ProxyCreationContext.setCurrentProxiedBeanName(null);//clear代理的名称 } }
/*	空判断	处理引介增强和普通增强器	 */	public static List
findAdvisorsThatCanApply(List
candidateAdvisors, Class
clazz) {
//空判断 if (candidateAdvisors.isEmpty()) {
return candidateAdvisors; } List
eligibleAdvisors = new ArrayList<>(); //首先处理引介增强,引介增强与其他普通增强处理不一样,所以单独处理 for (Advisor candidate : candidateAdvisors) {
if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
eligibleAdvisors.add(candidate); } } boolean hasIntroductions = !eligibleAdvisors.isEmpty(); for (Advisor candidate : candidateAdvisors) {
//引介增强已经处理 if (candidate instanceof IntroductionAdvisor) {
// already processed continue; } //对于普通bean的处理,canApply主要做匹配 if (canApply(candidate, clazz, hasIntroductions)) {
eligibleAdvisors.add(candidate); } } return eligibleAdvisors; }

获取Advisor

/*	暴露目标类	设置代理接口	封装所有的拦截器增强器加入proxyFactory	子类扩展	获取代理对象	 */	protected Object createProxy(Class
beanClass, @Nullable String beanName, @Nullable 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()) {
//确定对于给定的bean是否应该使用TargetClass而不是他的接口代理 if (shouldProxyTargetClass(beanClass, beanName)) {
proxyFactory.setProxyTargetClass(true); } else {
//添加代理接口 evaluateProxyInterfaces(beanClass, proxyFactory); } } //封装所有的拦截器增强器,统一封装成Advisor Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); //加入增强器 proxyFactory.addAdvisors(advisors); //设置要代理的类 proxyFactory.setTargetSource(targetSource); //定制代理,子类可进一步封装 customizeProxyFactory(proxyFactory); //用来控制代理工厂被配置之后,是否还允许修改通知 //默认false,在代理配置之后不允许修改代理的配置 proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) {
proxyFactory.setPreFiltered(true); } //获取代理对象 return proxyFactory.getProxy(getProxyClassLoader()); }
/*	注入拦截器,包括提前设置的interceptorNames	转换成Advisor	 */	protected Advisor[] buildAdvisors(@Nullable String beanName, @Nullable Object[] specificInterceptors) {
// Handle prototypes correctly... //注册所有的InterceptorName Advisor[] commonInterceptors = resolveInterceptorNames(); //设置是否在注入的拦截器之前加入 List allInterceptors = new ArrayList<>(); if (specificInterceptors != null) {
//加入拦截器 allInterceptors.addAll(Arrays.asList(specificInterceptors)); if (commonInterceptors.length > 0) {
if (this.applyCommonInterceptorsFirst) {
allInterceptors.addAll(0, Arrays.asList(commonInterceptors)); } else {
allInterceptors.addAll(Arrays.asList(commonInterceptors)); } } } if (logger.isTraceEnabled()) {
int nrOfCommonInterceptors = commonInterceptors.length; int nrOfSpecificInterceptors = (specificInterceptors != null ? specificInterceptors.length : 0); logger.trace("Creating implicit proxy for bean '" + beanName + "' with " + nrOfCommonInterceptors + " common interceptors and " + nrOfSpecificInterceptors + " specific interceptors"); } Advisor[] advisors = new Advisor[allInterceptors.size()]; for (int i = 0; i < allInterceptors.size(); i++) {
//拦截器转换成Advisor advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i)); } return advisors; }
/*	解析InterceptorNames并封装成Advisor	 */	private Advisor[] resolveInterceptorNames() {
BeanFactory bf = this.beanFactory; ConfigurableBeanFactory cbf = (bf instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) bf : null); List
advisors = new ArrayList<>(); for (String beanName : this.interceptorNames) {
if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) {
Assert.state(bf != null, "BeanFactory required for resolving interceptor names"); Object next = bf.getBean(beanName); //封装成Advisor advisors.add(this.advisorAdapterRegistry.wrap(next)); } } return advisors.toArray(new Advisor[0]); }/* 将AdvisorAdapter以及MethodInterceptor转换以及封装成Advisor */ public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
//如果封装对象是Advisor,无需处理 if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject; } //非Advisor和Advice无法封装 if (!(adviceObject instanceof Advice)) {
throw new UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof MethodInterceptor) {
// So well-known it doesn't even need an adapter. //使用DefaultPointcutAdvisor封装 return new DefaultPointcutAdvisor(advice); } //如果存在Advisor的适配器也需要封装 for (AdvisorAdapter adapter : this.adapters) {
// Check that it is supported. if (adapter.supportsAdvice(advice)) {
return new DefaultPointcutAdvisor(advice); } } throw new UnknownAdviceTypeException(advice); }

加载代理

public Object getProxy(@Nullable ClassLoader classLoader) {
return createAopProxy().getProxy(classLoader); }protected final synchronized AopProxy createAopProxy() {
//是否激活状态 if (!this.active) {
//激活代理配置 activate(); } //创建代理 return getAopProxyFactory().createAopProxy(this); }/* 创建CGLIB或者JDK动态代理 */ @Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
/* optimize:只对cglib有效,控制cglib是否采用激进的优化策略 proxyTargetClass:true则表示cglib代理,而且表示目标类本身被代理而不是目标类的接口 hasNoUserSuppliedProxyInterfaces:是否存在代理接口 */ //如果开启cglib激进策略,proxy-target-class=true(表示目标类本身被代理,采用cglib),存在代理接口 if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class
targetClass = config.getTargetClass(); if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " + "Either an interface or a target is required for proxy creation."); } //如果代理类是接口,targetClass是代理类 if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config); } return new ObjenesisCglibAopProxy(config); } else {
return new JdkDynamicAopProxy(config); } }

CGLIB代理

/*	设置CGLIB属性	获取拦截器链	设置拦截器链创建代理	 */	public Object getProxy(@Nullable ClassLoader classLoader) {
if (logger.isTraceEnabled()) {
logger.trace("Creating CGLIB proxy: " + this.advised.getTargetSource()); } try {
//获取目标类 Class
rootClass = this.advised.getTargetClass(); Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy"); Class
proxySuperClass = rootClass; //是否cglig代理类 if (ClassUtils.isCglibProxyClass(rootClass)) {
//获取父类 proxySuperClass = rootClass.getSuperclass(); //获取接口 Class
[] additionalInterfaces = rootClass.getInterfaces(); //添加接口到Advised for (Class
additionalInterface : additionalInterfaces) {
this.advised.addInterface(additionalInterface); } } // Validate the class, writing log messages as necessary. //验证class validateClassIfNecessary(proxySuperClass, classLoader); // Configure CGLIB Enhancer... //创建以及配置Enhancer Enhancer enhancer = createEnhancer(); if (classLoader != null) {
enhancer.setClassLoader(classLoader); if (classLoader instanceof SmartClassLoader && ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
enhancer.setUseCache(false); } } enhancer.setSuperclass(proxySuperClass); enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised)); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader)); //获取拦截器链 Callback[] callbacks = getCallbacks(rootClass); Class
[] types = new Class
[callbacks.length]; for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass(); } // fixedInterceptorMap only populated at this point, after getCallbacks call above //设置调用过滤器 enhancer.setCallbackFilter(new ProxyCallbackFilter( this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset)); enhancer.setCallbackTypes(types); // Generate the proxy class and create a proxy instance. //设置拦截器链,生成代理类,以及创建代理 return createProxyClassAndInstance(enhancer, callbacks); } catch (CodeGenerationException | IllegalArgumentException ex) {
throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() + ": Common causes of this problem include using a final class or a non-visible class", ex); } catch (Throwable ex) {
// TargetSource.getTarget() failed throw new AopConfigException("Unexpected AOP exception", ex); } }
/*	主要添加7个拦截器链	 */	private Callback[] getCallbacks(Class
rootClass) throws Exception {
// Parameters used for optimization choices... //对于exposeProxy的处理 boolean exposeProxy = this.advised.isExposeProxy(); boolean isFrozen = this.advised.isFrozen(); boolean isStatic = this.advised.getTargetSource().isStatic(); // Choose an "aop" interceptor (used for AOP calls). //将拦截器封装到DynamicAdvisedInterceptor中 Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised); // Choose a "straight to target" interceptor. (used for calls that are // unadvised but can return this). May be required to expose the proxy. Callback targetInterceptor; if (exposeProxy) {
targetInterceptor = (isStatic ? new StaticUnadvisedExposedInterceptor(this.advised.getTargetSource().getTarget()) : new DynamicUnadvisedExposedInterceptor(this.advised.getTargetSource())); } else {
targetInterceptor = (isStatic ? new StaticUnadvisedInterceptor(this.advised.getTargetSource().getTarget()) : new DynamicUnadvisedInterceptor(this.advised.getTargetSource())); } // Choose a "direct to target" dispatcher (used for // unadvised calls to static targets that cannot return this). Callback targetDispatcher = (isStatic ? new StaticDispatcher(this.advised.getTargetSource().getTarget()) : new SerializableNoOp()); //加入拦截器到callbacks Callback[] mainCallbacks = new Callback[] {
aopInterceptor, // for normal advice targetInterceptor, // invoke target without considering advice, if optimized new SerializableNoOp(), // no override for methods mapped to this targetDispatcher, this.advisedDispatcher, new EqualsInterceptor(this.advised), new HashCodeInterceptor(this.advised) }; Callback[] callbacks; // If the target is a static one and the advice chain is frozen, // then we can make some optimizations by sending the AOP calls // direct to the target using the fixed chain for that method. if (isStatic && isFrozen) {
Method[] methods = rootClass.getMethods(); Callback[] fixedCallbacks = new Callback[methods.length]; this.fixedInterceptorMap = new HashMap<>(methods.length); // TODO: small memory optimization here (can skip creation for methods with no advice) for (int x = 0; x < methods.length; x++) {
Method method = methods[x]; List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, rootClass); fixedCallbacks[x] = new FixedChainStaticTargetInterceptor( chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass()); this.fixedInterceptorMap.put(methods.toString(), x); } // Now copy both the callbacks from mainCallbacks // and fixedCallbacks into the callbacks array. callbacks = new Callback[mainCallbacks.length + fixedCallbacks.length]; System.arraycopy(mainCallbacks, 0, callbacks, 0, mainCallbacks.length); System.arraycopy(fixedCallbacks, 0, callbacks, mainCallbacks.length, fixedCallbacks.length); this.fixedInterceptorOffset = mainCallbacks.length; } else {
callbacks = mainCallbacks; } return callbacks; }

添加的拦截器添加的拦截器

查看第一个执行拦截器链DynamicAdvisedInterceptorintercept方法,其主要cglib调用增强

/*		串联调用链		责任链模式		 */		public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object oldProxy = null; boolean setProxyContext = false; Object target = null; TargetSource targetSource = this.advised.getTargetSource(); try {
if (this.advised.exposeProxy) {
// Make invocation available if necessary. oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } // Get as late as possible to minimize the time we "own" the target, in case it comes from a pool... target = targetSource.getTarget(); Class
targetClass = (target != null ? target.getClass() : null); //获取拦截器链 List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); Object retVal; // Check whether we only have one InvokerInterceptor: that is, // no real advice, but just reflective invocation of the target. if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
// We can skip creating a MethodInvocation: just invoke the target directly. // Note that the final invoker must be an InvokerInterceptor, so we know // it does nothing but a reflective operation on the target, and no hot // swapping or fancy proxying. Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args); //拦截器为空直接激活原方法 retVal = methodProxy.invoke(target, argsToUse); } else {
// We need to create a method invocation... //进入链 retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed(); } retVal = processReturnType(proxy, target, method, retVal); return retVal; } finally {
if (target != null && !targetSource.isStatic()) {
targetSource.releaseTarget(target); } if (setProxyContext) {
// Restore old proxy. AopContext.setCurrentProxy(oldProxy); } } }
@Override	@Nullable	public Object proceed() throws Throwable {
// We start with an index of -1 and increment early. //执行完所有增强后执行切点方法 if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint(); } //获取下一个执行的拦截器 Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex); if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have // been evaluated and found to match. //动态匹配 InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; Class
targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass()); if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
return dm.interceptor.invoke(this); } else {
// Dynamic matching failed. // Skip this interceptor and invoke the next in the chain. //不匹配则不执行拦截器 return proceed(); } } else {
// It's an interceptor, so we just invoke it: The pointcut will have // been evaluated statically before this object was constructed. //普通拦截器,直接调用,如: //MethodBeforeAdviceInterceptor //AspectJAroundAdvice //AspectJAfterAdvice //将this作为参数传递,保证当前实例中调用链的执行 return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); } }

JDK代理

@Override	@Nullable	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object oldProxy = null; boolean setProxyContext = false; TargetSource targetSource = this.advised.targetSource; Object target = null; try {
//对equals处理 if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself. return equals(args[0]); } //对hashCode处理 else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself. return hashCode(); } else if (method.getDeclaringClass() == DecoratingProxy.class) {
// There is only getDecoratedClass() declared -> dispatch to proxy config. return AopProxyUtils.ultimateTargetClass(this.advised); } else if (!this.advised.opaque && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) {
// Service invocations on ProxyConfig with the proxy config... return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args); } Object retVal; //目标对象内部调用将无法使用切面,则需要暴露代理 if (this.advised.exposeProxy) {
// Make invocation available if necessary. oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } // Get as late as possible to minimize the time we "own" the target, // in case it comes from a pool. target = targetSource.getTarget(); Class
targetClass = (target != null ? target.getClass() : null); // Get the interception chain for this method. //获取当前方法的拦截器链 List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); // Check whether we have any advice. If we don't, we can fallback on direct // reflective invocation of the target, and avoid creating a MethodInvocation. if (chain.isEmpty()) {
// We can skip creating a MethodInvocation: just invoke the target directly // Note that the final invoker must be an InvokerInterceptor so we know it does // nothing but a reflective operation on the target, and no hot swapping or fancy proxying. Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args); //如果没有发现任何拦截器那么直接调用切点方法 retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse); } else {
// We need to create a method invocation... //将拦截器封装ReflectiveMethodInvocation //便于使用其proceed进行链接表用拦截器 MethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // Proceed to the joinpoint through the interceptor chain. //执行拦截器链,这里主要执行前置和后置等 retVal = invocation.proceed(); } // Massage return value if necessary. Class
returnType = method.getReturnType(); //返回结果 if (retVal != null && retVal == target && returnType != Object.class && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
// Special case: it returned "this" and the return type of the method // is type-compatible. Note that we can't help if the target sets // a reference to itself in another returned object. retVal = proxy; } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException( "Null return value from advice does not match primitive return type for: " + method); } return retVal; } finally {
if (target != null && !targetSource.isStatic()) {
// Must have come from TargetSource. targetSource.releaseTarget(target); } if (setProxyContext) {
// Restore old proxy. AopContext.setCurrentProxy(oldProxy); } } }

转载地址:http://qnwoz.baihongyu.com/

你可能感兴趣的文章