spring源码分析之spring注解@Aspect是如何工作的?
2016-07-05 18:20
716 查看
1、@Aspect
在xml定义:<aop:aspectj-autoproxy />,其定义在http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
1.1 注册
的继承关系如下:
1.2 解析过程
AspectJAutoProxyBeanDefinitionParser.java#parse()方法
注册过程:
调用实现类:
1.3 具体实现类为:AbstractAutoProxyCreator的postProcessAfterInitialization()方法
DefaultAopProxyFactory#createAopProxy()方法
默认使用jdk自带的代理,还有一种cglib方式。
在xml定义:<aop:aspectj-autoproxy />,其定义在http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- <xsd:element name="aspectj-autoproxy"> - <xsd:annotation> - <xsd:documentation source="java:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"> - <![CDATA[ Enables the use of the @AspectJ style of Spring AOP. ]]> </xsd:documentation> </xsd:annotation> - <xsd:complexType> - <xsd:sequence> - <xsd:element name="include" type="includeType" minOccurs="0" maxOccurs="unbounded"> - <xsd:annotation> - <xsd:documentation> - <![CDATA[ Indicates that only @AspectJ beans with names matched by the (regex) pattern will be considered as defining aspects to use for Spring autoproxying. ]]> </xsd:documentation> </xsd:annotation> </xsd:element> </xsd:sequence> - <xsd:attribute name="proxy-target-class" type="xsd:boolean" default="false"> - <xsd:annotation> - <xsd:documentation> - <![CDATA[ Are class-based (CGLIB) proxies to be created? By default, standard Java interface-based proxies are created. ]]> </xsd:documentation> </xsd:annotation> </xsd:attribute> - <xsd:attribute name="expose-proxy" type="xsd:boolean" default="false"> - <xsd:annotation> - <xsd:documentation> - <![CDATA[ Indicate that the proxy should be exposed by the AOP framework as a ThreadLocal for retrieval via the AopContext class. Off by default, i.e. no guarantees that AopContext access will work. ]]> </xsd:documentation> </xsd:annotation> </xsd:attribute> </xsd:complexType> </xsd:element>
1.1 注册
org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
的继承关系如下:
1.2 解析过程
AspectJAutoProxyBeanDefinitionParser.java#parse()方法
public BeanDefinition parse(Element element, ParserContext parserContext) { AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element); extendBeanDefinition(element, parserContext); return null; }
注册过程:
public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary( ParserContext parserContext, Element sourceElement) { BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary( parserContext.getRegistry(), parserContext.extractSource(sourceElement)); useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement); registerComponentIfNecessary(beanDefinition, parserContext); }
调用实现类:
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, Object source) { return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source); }
1.3 具体实现类为:AbstractAutoProxyCreator的postProcessAfterInitialization()方法
DefaultAopProxyFactory#createAopProxy()方法
@Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { 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."); } if (targetClass.isInterface()) { return new JdkDynamicAopProxy(config); } return new ObjenesisCglibAopProxy(config); } else { return new JdkDynamicAopProxy(config); } }
默认使用jdk自带的代理,还有一种cglib方式。
相关文章推荐
- 【Web API2】ASP.NET Web API Security
- 基于.Net Framework 4.0 Web API开发(3):ASP.NET Web APIs 异常的统一处理Attribute 和统一写Log 的Attribute的实现
- 未将对象引用设置到对象实例(转)
- ASP.NET MVC Html.BeginForm
- asp.net spring.net+mvc 和j2ee spring mvc的思考
- 微信公众号token的asp.net脚本
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (六)写在后面的话
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (五)测试你的成果
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (四)配置全攻略
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (二)创建你的项目
- 让整个table居于页面正中,(上下左右居中)。
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (一)搭建你的环境
- ASP.NET Core 介绍
- ASP.NET Core 介绍
- asp.net : 拒绝频繁的IP访问
- 使用jasperreports-5.6.0.jar导致的问题
- ASP.NET的缓存机制
- Android AOP 之AspectJ(一)
- asp.net Core开启全新的时代,用视频来告诉你,学习就是这么SO easy。