zoukankan      html  css  js  c++  java
  • AcpectJ注释方式配置AOP

    1.AspectJ的概念
      @AspectJ类似于Java注解的普通Java类
      Spring可以使用AspectJ来做切入点解析
      AOP的运行时仍旧是纯的Spring AOP,对AspectJ的编译器或者织入无依赖性.
    2.配置方式
      注解方式 -- @Configuration
                  @EnableAspectJAutoProxy
                  任何拥有@Aspect注解的bean都将被Spring自动识别并应用
                  注释的类可以有方法和字段,他们也可以有切入点(pointcut),通知(Advice)和引入(introduction)声明
                  @Aspect注解不能够通过类路径自动检测发现,需要配合使用@Component注释或者在XML配置bean
      xml文件方式
               --<aop:aspectj-autoproxy/>
               
    3.ASpectJ为编译期的AOP,检查代码并匹配连接点与切入点的代价昂贵
      好的切入点包括
        --选择特定类型的连接点(execution/get/set/call/handler)
        --确认连接点范围(within/withincode)
        --匹配上下文信息(this/target/@annotation)
        
    4.Around advice
      使用@Around注释来声明,通知方法的第一个参数必须是ProcessdingJoinPoint类型
      再通知内部调用processdingjoinpoint的proceed()方法会引导至真正的方法,传入一个Object[]对象,数组中的值将被作为参数传递给方法
      
    5. * 使用execution表达式
    * 使用已经定义表达式的方法名 --@Before("pointcut()")
    @AfterReturning(pointcut="implPointcut()",returning="args")
    @AfterThrowing(pointcut="pointcut()",throwing="e")
    @After("pointcut()")
          @Pointcut("execution(* com.aspectj.impl.*Impl.*(..))")
          @Pointcut("within(com.aspectj.impl.*)")
          
    6.Advice扩展
      1>给advice传递参数 -- 方法的参数可以是任何类的对象
        在@before时+&&args(account,..)
        定义注解传参
            @Before("pointcut()&&@annotation(methodValue)")
            public void beforeWithAnnotation(Methodvalue methodValue)
        SpringAOp可以处理泛型类的声明和使用方法的参数
        通知和切入点注解有一个额外的"argNames"属性,它可以用来指定所注解的方法的参数名
        --如果第一参数时JoinPoint,ProceedingJoinPoint,JoinPoint.StaticPart,那么可以忽略它 -- -- ,argNames="bean,auditable,.."
      2>Introductions
        允许一个切面声明一个通知对象实现指定接口,并且提供了一个接口实现类来代表这些对象
        introduction使用@DeclareParents进行注解,这个注解用来定义匹配的类型拥有一个新的parent
      3>切面实例化模型 -- 高级主题
        "perthis"切面通过指定@Aspect注解perthis子句实现
        每个独立的service对象执行时都会创建一个切面实例
        service对象的每个方法在第一次执行的时候创建切面实例,切面在service对象失效的同时失效
        
    http://blog.csdn.net/jacxuan/article/details/53454819

        
  • 相关阅读:
    iOS --- UIColor中使用16进制选取颜色
    我的投资、理財、財富观
    CentOS7.1 KVM虚拟化之虚拟机快照(5)
    C++实现简单的内存块自己主动管理
    SQL Server,Access数据库查询易混点和C#中parameter指定参数长度的优缺点
    【转】Android Building System 总结
    【转】Android ROM研究---Android build system增加模块
    【转】Android编译系统详解(三)——编译流程详解
    【转】单独编译android framework模块出现的问题
    【转】Android 驱动开发系列四
  • 原文地址:https://www.cnblogs.com/qwop/p/6637316.html
Copyright © 2011-2022 走看看