04-ProxyFactoryBean&AspectProxyFactory分析

ProxyFactoryBean&AspectProxyFactory分析

​ 上篇使用ProxyCreatorSupport的实现类ProxyFactory结合案例剖析了SpringAop原生方式底层运行原理。之前也提到了ProxyFactoryBean、AspectProxyFactoryProxyFactory相比只是增加了一些feture, 本节就来剖析下这两个实现类在原生的基础上做了哪些事情。

ProxyFactoryBean
public class ProxyFactoryBean extends ProxyCreatorSupport
		implements FactoryBean<Object>, BeanClassLoaderAware, BeanFactoryAware {
		
	......
	
	//与ProxyFactory相比ProxyFactoryBean新增了interceptorNames这个属性
	//该属性可以指定多个 Advice\Advisor\Interceptor类型的Bean的name
	private String[] interceptorNames;
    
    private boolean singleton = true;
    
    private transient BeanFactory beanFactory;
    
    private boolean advisorChainInitialized = false;
    
    //保存ProxyFactoryBean#getObject返回的代理对象
    private Object singletonInstance;
    
    //之前文章遇到过这个AdvisorAdapterRegistry,将Advice包装为Advisor
    private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
    
    
    @Override
	@Nullable
	public Object getObject() throws BeansException {
		//初始化interceptorNames属性中指定的Advice、Advisor等Bean
		initializeAdvisorChain();
		if (isSingleton()) {
			return getSingletonInstance(); //返回代理对象
		}
		else {
			if (this.targetName == null) {
				logger.info("Using non-singleton proxies with singleton targets is ...");
			}
			return newPrototypeInstance();
		}
	}
	
	
	private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
		
		//防止并发初始化AdvisorChain
		if (this.advisorChainInitialized) {
			return;
		}
		
		//如果interceptorNames属性有设置bean名称,则进行处理
		if (!ObjectUtils.isEmpty(this.interceptorNames)) {
			if (this.beanFactory == null) {
				throw new IllegalStateException("No BeanFactory available ..."));
			}

			if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) &&
					this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {
				throw new AopConfigException("Target required after globals");
			}

			//for循环处理指定的bean名称
			for (String name : this.interceptorNames) {
				
				//处理定义的模糊bean名称,例如 *Service
                //这边会将匹配到的所有Bean包装为Advisor都添加进AdvisedSupport#advisors属性中
				if (name.endsWith(GLOBAL_SUFFIX)) {
					if (!(this.beanFactory instanceof ListableBeanFactory)) {
						throw new AopConfigException(
								"Can only use global advisors or interceptors ...");
					}
					addGlobalAdvisor((ListableBeanFactory) this.beanFactory,
							name.substring(0, name.length() - GLOBAL_SUFFIX.length()));
				}

				else {//处理其他精确定义的Bean名称
					Object advice;
					if (this.singleton || this.beanFactory.isSingleton(name)) {
						advice = this.beanFactory.getBean(name);
					}
					else {
						advice = new PrototypePlaceholderAdvisor(name);
					}
					//将获取到的Bean包装为Advisor
					addAdvisorOnChainCreation(advice, name);
				}
			}
		}

		this.advisorChainInitialized = true;
	}
	
	//该方法主要是根据interceptorNames属性获取到的Advice、Advisor类型的Bean
	//将这些Bean统一包装为Advisor类型添加进AdvisedSupport#advisors属性中 (这个步骤和ProxyFactory中是一样的)
	private void addAdvisorOnChainCreation(Object next, String name) {
		Advisor advisor = namedBeanToAdvisor(next);
		if (logger.isTraceEnabled()) {
			logger.trace("Adding advisor with name '" + name + "'");
		}
		addAdvisor(advisor);
	}
	
	//使用AdvisorAdapterRegistry将Bean统一包装为Advisor
	private Advisor namedBeanToAdvisor(Object next) {
		try {
			return this.advisorAdapterRegistry.wrap(next);
		}
		catch (UnknownAdviceTypeException ex) {
			throw new AopConfigException("Unknown advisor type ....", ex);
		}
	}
	
	//核心方法 : 生成代理类并返回
	private synchronized Object getSingletonInstance() {
		if (this.singletonInstance == null) {
			//实时获取目标对象实例
			this.targetSource = freshTargetSource();
			if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
				Class<?> targetClass = getTargetClass();
				if (targetClass == null) {
					throw new FactoryBeanNotInitializedException("Cannot determine ...");
				}
				//获取目标对象的接口并设置到AdvisedSupport#interfaces属性中
				//这个步骤和ProxyFactory中是一样的,用于保存代理接口
				setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
			}
			super.setFrozen(this.freezeProxy);
			//生成代理类并返回
			this.singletonInstance = getProxy(createAopProxy());
		}
		return this.singletonInstance;
	}
	
	private TargetSource freshTargetSource() {
		if (this.targetName == null) {
			if (logger.isTraceEnabled()) {
				logger.trace("Not refreshing target: Bean name not .....");
			}
			return this.targetSource;
		}
		else {
			if (this.beanFactory == null) {
				throw new IllegalStateException("No BeanFactory available ....");
			}
			Object target = this.beanFactory.getBean(this.targetName);
			return (target instanceof TargetSource ? (TargetSource) target : new SingletonTargetSource(target));
		}
	}
	
	
	.......
    
}

​ 如上是对ProxyFactoryBean核心源码的分析,从分析中可以看出它和ProxyFactory相比新增了一个interceptorNames属性,该属性可以指定一个Advice或Advisor类型的Beanbean名称数组,ProxyFactoryBean#getObject()方法会在生成目标对象的AOP代理之前,先把这个属性中指定的bean名称对应的BeanAdvisor或者Advice类型)使用DefaultAdvisorAdapterRegistry统一将它们转为Advisor类型后添加到之前分析过的AdvisedSupport#advisors属性中。处理完interceptorNames属性后就和ProxyFactory的逻辑一样了 : 生成代理对象。

AspectJProxyFactory

AspectJProxyFactory的逻辑比较多此处就不再贴代码了,总的来说可以分为以下步骤 :

  • 首先将将切面中被@Pointcut、@After、@Before、@AfterThrowing、@Around、@AfterReturning注解修饰的方法包装为切点和相应类型的Advice.如下图所示ReflectiveAspectJAdvisorFactory#getAdvice方法是转Advice的核心逻辑。@Pointcut切点的解析也类似。
public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFactory implements Serializable {

	......
	
	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 (!isAspect(candidateAspectClass)) {
			throw new AopConfigException("Advice must be declared inside an aspect");
		}

		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:
				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);
		}

		springAdvice.setAspectName(aspectName);
		springAdvice.setDeclarationOrder(declarationOrder);
		String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
		if (argNames != null) {
			springAdvice.setArgumentNamesFromStringArray(argNames);
		}
		springAdvice.calculateArgumentBindings();

		return springAdvice;
	}
	
	
	......
}
  • 第二步是将第一步中获取到的每个Advice和其切点组合成一个Advisor(具体的Advisor类型是InstantiationModelAwarePointcutAdvisorImpl).这一步的具体逻辑可以参考AspectJProxyFactory#addAdvisorsFromAspectInstanceFactory方法中的第一行代码,这行代码又将逻辑委托给了ReflectiveAspectJAdvisorFactory#getAdvisors方法

    private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) {
    		//获取到当前定义所有Advisor
    		List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory);
    		Class<?> targetClass = getTargetClass();
    		Assert.state(targetClass != null, "Unresolvable target class");
    		//从所有的Advisor中获取到需要对当前类拦截的Advisor
    		advisors = AopUtils.findAdvisorsThatCanApply(advisors, targetClass);
    		//向对当前类有效的Advisor列表表头添加ExposeInvocationInterceptor拦截器,下面会讲到
    		AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);
    		AnnotationAwareOrderComparator.sort(advisors);
    		//将对当前类有效的Advisor添加到AdvisedSupport#advisors属性中,最后生成代理对象
    		addAdvisors(advisors);
    	}
    
  • 第三步是在第二步获取到的List<Advisor>基础上,向列表的头部添加一个ExposeInvocationInterceptor.ADVISOR类型的Advisor. 逻辑在上面所示的AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);中。这个Advisor相当于是一个保存当前连接点MethodInvocation信息的ThreadLocal。 因为第一步获取到的各个Advice在执行时需要使用连接点相关信息,所以ExposeInvocationInterceptor的放在首位的目的就是供其他Advice获取连接点信息。

  • 第四步就和之前分析的ProxyFactory逻辑一样了 : 将第三步中的List<Advisor>添加到AdvisedSupport#advisors属性中,生成代理对象并返回。

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Spring5的配置文件,该文件配置了Spring5 AOP中的通知,请分析代码并回答后面的问题。(10分) ‎ ​applicationContext.xml: ‎ ​① <?xml version="1.0" encoding="UTF-8"?> ‎ ​② <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" ‎ ​ "https://www.springframework.org/dtd/spring-beans-2.0.dtd" [ ‎ ​<!ENTITY contextInclude SYSTEM ‎ ​ "org/springframework/web/context/WEB-INF/contextInclude.xml">]> ‎ ​③ <beans> ‎ ​④  <bean id="BeforeAdvice" ‎ ​⑤  class="adviceexample.GettingBeforeAdvice"></bean> ‎ ​⑥  <bean id="AfterAdvice" class="adviceexample.GettingAfterAdvice"></bean> ‎ ​⑦  <bean id="proxy" ‎ ​⑧  class="org.springframework.aop.framework.ProxyFactoryBean"> ‎ ​⑨  <property name="proxyInterfaces" ‎ ​⑩  value="adviceexample.Reception"></property> ‎ ​⑪  <property name="interceptorNames"> ‎ ​⑫  <list> ‎ ​⑬  <value>BeforeAdvice</value> ‎ ​⑭  <value>AfterAdvice</value> ‎ ​⑮  </list> ‎ ​⑯  </property> ‎ ​⑰  <property name="target" ref="target"></property> ‎ ​⑱  </bean> ‎ ​⑲  <bean id="target" class="adviceexample.ConcreteReception"></bean> ‎ ​⑳ </beans> ‎ ​(1)上面文件配置了的两种通知是用来做什么的(4分) ‎ ​(2)上面文件配置了代理工厂对象proxy,该对象有三个属性,依次是代理接口,通知和目标对象,其中,代理接口是什么?(2分) ‎ ​(3)分析上面代码说明AOP主要包含哪些内容?(4分)
05-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值