1、@Aspect

在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方式。

spring源码分析之spring注解@Aspect是如何工作的?的更多相关文章

  1. Spring源码分析-从@ComponentScan注解配置包扫描路径到IoC容器中的BeanDefinition,经历了什么(一)?

    阅前提醒 全文较长,建议沉下心来慢慢阅读,最好是打开Idea,点开Spring源码,跟着下文一步一步阅读,更加便于理解.由于笔者水平优先,编写时间仓促,文中难免会出现一些错误或者不准确的地方,恳请各位 ...

  2. Spring源码分析:Spring IOC容器初始化

    概述: Spring 对于Java 开发来说,以及算得上非常基础并且核心的框架了,在有一定开发经验后,阅读源码能更好的提高我们的编码能力并且让我们对其更加理解.俗话说知己知彼,百战不殆.当你对Spri ...

  3. spring源码分析之cache注解

    Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache),而是一个对缓存使用的抽象 ...

  4. 【spring源码分析】spring和@PostConstruct注解

    @PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...

  5. 【spring源码分析】@Value注解原理

    class org.springframework.context.support.PropertySourcesPlaceholderConfigurer 该类实现了的接口:1.org.spring ...

  6. 【spring源码分析】spring ioc容器之前生今世--DefaultListableBeanFactory源码解读

    spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说, DefaultL ...

  7. 【spring源码分析】spring AspectJ的Execution表达式

    在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execution (* com.sam ...

  8. spring源码分析之spring jmx

    JMX架构定义: https://docs.oracle.com/javase/8/docs/technotes/guides/jmx/overview/architecture.html Archi ...

  9. 【spring源码分析】spring关于循环依赖的问题

    引言:循环依赖就是N个类中循环嵌套引用,如果在日常开发中我们用new 对象的方式发生这种循环依赖的话程序会在运行时一直循环调用,直至内存溢出报错.下面说一下Spring是如果解决循环依赖的. 第一种: ...

随机推荐

  1. android wifi P2P CONNECT, INVITE和JOIN流程选择

    android wifi P2P CONNECT, INVITE和JOIN流程选择

  2. php图片处理类库 Image

    image 下载地址 https://github.com/Intervention/image.git 下载之后解压 执行composer update 生成 autoload.php文件   该类 ...

  3. Arc Engine下数据的加载处理

    1.加载Shapefile数据 IWorkspaceFactory pWorkspaceFactory; IFeatureWorkspace pFeatureWorkspace; IFeatureLa ...

  4. Atom.io设置ctrl+delete

    一般常见的text editor,在文本前面的空白处按下ctrl+delete,只是删除空白符到单词前面停下,但是Atom.io的默认设置,把空白符后遇到的第一个单词也删掉了.改配置方法是在keyma ...

  5. Linux环境下中文字体乱码处理办法

    项目中包含了一项生成JPG图片的功能,该功能需要使用JAVA的Graphics2D动态生成图片中的文字,原来在Windows平台中没有发现问题,但是迁移至Linux平台后发现生成的中文变成了乱码. 百 ...

  6. GnuPG 1.4.15 发布,邮件加密工具

    GnuPG 1.4.15 改进包括: * Fixed possible infinite recursion in the compressed packet parser. [CVE-2013-44 ...

  7. 讲讲HashCode的作用

    前言 Object提供给我们了一个Native的方法“public native int hashCode();”,本文讲讲Hash是什么以及HashCode的作用 Hash 先用一张图看下什么是Ha ...

  8. JavaScript使用DeviceOne开发实战(五)仿ZAKER应用

    关于index底下切换的的组件,可以用ViewShower实现,详见: do_ViewShower http://bbs.deviceone.net/forum.php?mod=viewthread& ...

  9. 作业七:团队项目——Alpha版本冲刺阶段-07

    昨天进展:代码编写. 今天安排:代码编写.

  10. 如何捕获和分析 JavaScript Error

    前端工程师都知道 JavaScript 有基本的异常处理能力.我们可以 throw new Error(),浏览器也会在我们调用 API 出错时抛出异常.但估计绝大多数前端工程师都没考虑过收集这些异常 ...