8.4.2 使用AspectJ实现AOP

        AspectJ是一个基于Java语言的AOP框架。Spring 4.0 的AOP对AspectJ很好的集成。

        AspectJ是Java 语言的一个AOP实现,其主要包括两个部分:一个部分定义了如何表达、定义AOP编程中的语法规范,通过这套语法规范,可以方便地用AOP来解决Java语言中存在的交叉关注点 的问题;另一个部分是工具部分,包括编译器、调试工具等。

        1. 下载和安装AspectJ:java -jar aspectj-x.x.x.jar

        2.AspectJ使用入门

          ⊙ bin : 该路径下存放了aj、aj5、ajc、ajdoc、ajbrowser等命令,其中ajc命令最常用,它的作用类似于javac,用于对普通的Java类进行编译时增强。

          ⊙ docs : 该路径下存放了AspectJ的使用说明、参考手册、API文档等文档。

          ⊙ lib : 该路径下的4个JAR文件是AspectJ的核心类库。

          ⊙ 相关授权文件。

          Class : Hello

package edu.pri.lime._8_4_2.service;

public class Hello {

    public void foo(){
System.out.println("执行Hello组件的foo()方法...");
}
public int addUser(String name,String pass){
System.out.println("执行Hello组件的addUser()添加用户 : " + name);
return 20;
}
}

          Class : World

package edu.pri.lime._8_4_2.service;

public class World {

    public void bar(){
System.out.println("执行World组件的bar()方法...");
}
}

          Class : AspectJTest

package edu.pri.lime._8_4_2.service;

public class AspectJTest {

    public static void main(String[] args) {

        Hello hello = new Hello();
hello.foo();
hello.addUser("lime", "7878,");
World world = new World();
world.bar();
}
}

          AspectJ : AuthAspect

package edu.pri.lime._8_4_2.service;

public aspect AuthAspect {

//   在所有业务方法之前执行
//   指定在执行edu.pri.lime._8_4_2.service包中任意类的任意方法之前执行下面代码块
//   * : 返回值不限;* :类名不限; * : 方法名不限;(..) : 任意个数、类型不限的形参

    before() : execution(* edu.pri.lime._8_4_2.service.*.*(..)){

        System.out.println("模拟进行权限检查...");
}
}

          AspectJ : LogAspect

package edu.pri.lime._8_4_2.service;

public aspect LogAspect {

//    定义一个Pointcut,其名为logPointcut
// 该Pointcut代表了后面给出的切入点表达式,这样可复用该切入点表达式
pointcut logPointcut() : execution(* edu.pri.lime._8_4_2.service.*.*(..));
after() : logPointcut(){
System.out.println("模拟记录日志...");
}
}

          AspectJ : TxAspect

package edu.pri.lime._8_4_2.service;

public aspect TxAspect {

//    指定执行Hello.sayHello()方法时执行下面的代码块
Object around():call(* edu.pri.lime._8_4_2.service.*.*(..)){
System.out.println("模拟开启事务...");
// 回调原来的目标方法
Object rvt = proceed();
System.out.println("模拟结束事务...");
return rvt;
}
}

          AOP实现可分为两类(按AOP框架修改源代码的时机)

            ⊙ 静态AOP实现 : AOP框架在编译阶段对程序进行修改,即实现对目标类的增强,生成讲台的AOP代理类(生成的*.class 文件已经被改掉了,需要使用特定的编译器).以AspectJ为代表。

            ⊙ 动态AOP实现 : AOP框架在运行阶段动态生成AOP代理(在内存中以JDK动态代理或cglib动态第生成AOP代理类),以实现对目标对象的增强。以Spring AOP为代表。

          一般来说,静态AOP实现具有较好的性能,但需要使用特殊的编译器。动态AOP实现是纯Java实现,因此无须特殊的编译器,但是通常性能略差。

啦啦啦

啦啦啦

啦啦啦

啦啦啦

啦啦啦

8 -- 深入使用Spring -- 4...2 使用AspectJ实现AOP的更多相关文章

  1. Spring @AspectJ 实现AOP 入门例子(转)

    AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): package com.samter.common ...

  2. Spring框架(6)---AspectJ实现AOP

    AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...

  3. Spring详解(五)------AspectJ 实现AOP

    上一篇博客我们引出了 AOP 的概念,以及 AOP 的具体实现方式.但是为什么要这样实现?以及提出的切入点表达式到底该怎么理解? 这篇博客我们通过对 AspectJ 框架的介绍来详细了解. 1.什么是 ...

  4. Spring详解(六)------AspectJ 实现AOP

    上一篇博客我们引出了 AOP 的概念,以及 AOP 的具体实现方式.但是为什么要这样实现?以及提出的切入点表达式到底该怎么理解? 这篇博客我们通过对 AspectJ 框架的介绍来详细了解. 1.什么是 ...

  5. Spring基于AspectJ的AOP的开发——注解

    源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...

  6. Spring整合AspectJ的AOP

    学而时习之,不亦说乎!                              --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...

  7. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  8. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

随机推荐

  1. java中GZIPOutputStream 流的使用(EOFException)

    GZip流的使用非常多人都出现了以下的异常:java.io.EOFException: Unexpected end of ZLIB input stream.或者出现压缩后的数据不全的情况(就是压缩 ...

  2. 将windows控制台内容输出到文件中

    将windows控制台内容输出到文件中 dir>c:/file.txt 2>&1   对应的java  class   >c:/file.txt 2>&1   ...

  3. Urllib3 库详解

    文档:http://urllib3.readthedocs.io/en/latest/

  4. ViewPager一屏显示多个item,及边缘滑动事件优化

    关于ViewPager显示两边的item方法,网络上是方法都在ViewPager外包一个Layout, 然后设置ViewPager和外面的Layout的clipChildren="false ...

  5. 解决android studio上“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65935”问题

    我是在更换应用的一个jar包时发生的这个错误,网上查到说是因为同时在工程中引入了多个第三方jar包,导致调用的方法数超过了android设定的65935个(DEX 64K problem),进而导致d ...

  6. Namenode HA原理详解(脑裂)

    转自:http://blog.csdn.net/tantexian/article/details/40109331 Namenode HA原理详解 社区hadoop2.2.0 release版本开始 ...

  7. Self_Java + Selenium + Maven 环境搭建步骤

    转自:http://www.jianshu.com/p/3c05e8c9ee81 我们使用Java+Selenium WebDriver 来进行环境的搭建,同样分为两个部分: 安装Java 和 int ...

  8. 00-02.PHP 网站假设 之 学习PHP语法 [James建站]

    PHP 手册 Stig Sæther Bakken Alexander Aulbach Egon Schmid Jim Winstead Lars Torben Wilson Rasmus Lerdo ...

  9. Assets/FollowDestination.cs(6,13): error CS0246: The type or namespace name `NavMeshAgent' could not be found. Are you missing `UnityEngine.AI' using directive?的解决方案

    问题的出现与描述 在Unity中创建一个NPC,使它一直跟踪一个目标Destination,C#脚本代码如下,错误信息描述如下 using System.Collections; using Syst ...

  10. Wireshark 抓包小例子

    捕捉过滤器(CaptureFilters): 用于决定将什么样的信息记录在捕捉结果中. 语法详见:http://www.cnblogs.com/SZxiaochun/p/7920962.html 显示 ...