欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring源码学习系列4-3种常用的自动代理创建器 博客分类: spring spring 

程序员文章站 2024-02-12 20:17:04
...
3种自动代理器是
AnnotationAwareAspectJAutoProxyCreator-<aop:aspectj-autoproxy />

AbstractAdvisorAutoProxyCreator-<aop:config>

InfrastructureAdvisorAutoProxyCreator-<tx:annotation-driven/>

他们在Spring中注册的beanName都是
public static final String AUTO_PROXY_CREATOR_BEAN_NAME =
			"org.springframework.aop.config.internalAutoProxyCreator"

它们的优先级从上到下,即3者都配置了,默认注册AnnotationAwareAspectJAutoProxyCreator。那<tx:annotation-driven/>如何解析呢




<aop:aspectj-autoproxy /> - AopNamespaceHandler

该配置就是定义了一个AnnotationAwareAspectJAutoProxyCreator(BeanPostProcessor)的bean

InstantiationModelAwarePointcutAdvisor
AspectJExpressionPointcut
AspectJMethodBeforeAdvice,AspectJAfterAdvice,AspectJAfterReturningAdvice,AspectJAfterThrowingAdvice,AspectJAroundAdvice



该<aop>配置,不会定义Advisor,定义Advisor是在后置处理器(AnnotationAwareAspectJAutoProxyCreator)生成代理时动态创建的

AnnotationAwareAspectJAutoProxyCreator#findCandidateAdvisors
@Override
	protected List<Advisor> findCandidateAdvisors() {
		// Add all the Spring advisors found according to superclass rules.
		List<Advisor> advisors = super.findCandidateAdvisors();
		// Build Advisors for all AspectJ aspects in the bean factory.
		advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
		return advisors;
	}


this.aspectJAdvisorsBuilder.buildAspectJAdvisors()生成InstantiationModelAwarePointcutAdvisor,表示注解或Apect类型的切面。

相关切面元素:
AspectJExpressionPointcut
(ReflectiveAspectJAdvisorFactory#getPointcut)

AspectJMethodBeforeAdvice,AspectJAfterAdvice,AspectJAfterReturningAdvice,AspectJAfterThrowingAdvice,AspectJAroundAdvice
(ReflectiveAspectJAdvisorFactory#getAdvice)


this.aspectJAdvisorsBuilder.buildAspectJAdvisors()具体如何设计实现注解的切面这里不做扩展,感兴趣的可以通过代码查看










<aop:config/> - AopNamespaceHandler

spring中的该配置实际上就是注册了几个beanDefinition

AspectJAwareAdvisorAutoProxyCreator -BeanPostProcessor

AspectJExpressionPointcut - Pointcut

DefaultBeanFactoryPointcutAdvisor - Advisor



至于怎么实现解析注册的,感兴趣的可查看源码
ConfigBeanDefinitionParser#parse
public BeanDefinition parse(Element element, ParserContext parserContext) {
		CompositeComponentDefinition compositeDef =
				new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
		parserContext.pushContainingComponent(compositeDef);

		configureAutoProxyCreator(parserContext, element);

		List<Element> childElts = DomUtils.getChildElements(element);
		for (Element elt: childElts) {
			String localName = parserContext.getDelegate().getLocalName(elt);
			if (POINTCUT.equals(localName)) {
				parsePointcut(elt, parserContext);
			}
			else if (ADVISOR.equals(localName)) {
				parseAdvisor(elt, parserContext);
			}
			else if (ASPECT.equals(localName)) {
				parseAspect(elt, parserContext);
			}
		}

		parserContext.popAndRegisterContainingComponent();
		return null;
	}











<tx:annotation-driven transaction-manager="transactionManager" /> - TxNamespaceHandler

spring中的该配置实际上就是注册了几个beanDefinition

InfrastructureAdvisorAutoProxyCreator

TransactionAttributeSourcePointcut Pointcut

TransactionInterceptor - MethodInterceptor(advice)

TransactionAttributeSourceAdvisor - Advisor(持有advice,pointcut)


TransactionInterceptor主要是处理事务的增强,这里不对其扩展
TransactionInterceptor#invoke
public Object invoke(final MethodInvocation invocation) throws Throwable {
		// Work out the target class: may be {@code null}.
		// The TransactionAttributeSource should be passed the target class
		// as well as the method, which may be from an interface.
		Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);

		// Adapt to TransactionAspectSupport's invokeWithinTransaction...
		return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() {
			public Object proceedWithInvocation() throws Throwable {
				return invocation.proceed();
			}
		});
	}






参考:<spring源码学习系列1.2-spring事务代理深入分析2>
http://newjava-sina-cn.iteye.com/blog/2422939
相关标签: spring