• 那是从何处传来的钟声呢?偶尔听到那钟声,平添一份喜悦与向往之情。

Spring之AOP详解

后端 Nanait 12年前 (2012-11-24) 946次浏览 已收录 0个评论 扫描二维码

AOP 概念

1 aop:面向切面(方面)编程,扩展功能不修改源代码实现

2  AOP 采取横向抽取机制,取代了传统纵向继承体系重复性代码

3 aop 底层使用动态代理实现

(1)第一种情况,有接口情况,使用动态代理创建接口实现类代理对象

(2)第二种情况,没有接口情况,使用动态代理创建类的子类代理对象

 

AOP 原理

画图分析原理(图片看不清,可以 放大浏览器)

Spring 之 AOP 详解

图 1

 

Spring 之 AOP 详解

图 2

Spring 之 AOP 详解

图 3

AOP 操作术语

Joinpoint(连接点): 类里面可以被增强的方法,这些方法称为连接点 Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义.

Advice(通知/增强):所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)

Aspect(切面): 是切入点和通知(引介)的结合

Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field.

Target(目标对象):代理的目标对象(要增强的类)

Weaving(织入):是把增强应用到目标的过程.

把 advice 应用到 target 的过程

Proxy(代理):一个类被 AOP 织入增强后,就产生一个结果代理类

Spring 之 AOP 详解

图 4

Spring 的 aop 操作

1 在 spring 里面进行 aop 操作,使用 aspectj 实现

(1)aspectj 不是 spring 一部分,和 spring 一起使用进行 aop 操作

(2)Spring2.0 以后新增了对 AspectJ 支持

 

2 使用 aspectj 实现 aop 有两种方式

(1)基于 aspectj 的 xml 配置

(2)基于 aspectj 的注解方式

 

Aop 操作准备

1 除了导入基本的 jar 包之外,还需要导入 aop 相关的 jar 包

Spring 之 AOP 详解

图 5

2 创建 spring 核心配置文件,导入 aop 的约束

Spring 之 AOP 详解

图 6

使用表达式配置切入点

1 切入点:实际增强的方法

2 常用的表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

(1)execution(* cn.itcast.aop.Book.add(..))

(2)execution(* cn.itcast.aop.Book.*(..))

(3)execution(* *.*(..))

(4) 匹配所有 save 开头的方法 execution(* save*(..))

Aspectj 的 aop 操作(xml 文件方式)

Spring 之 AOP 详解

图 7

Spring 之 AOP 详解

图 8

Spring 之 AOP 详解

图 9

 

Spring 之 AOP 详解

图 10

 

 

完整代码如下

  • Book.java   普通的类,但是方法还不完善
  1. package com.liuyanzhao.aop;
  2. public class Book {
  3.     public void add() {
  4.         System.out.println(“add………..”);
  5.     }
  6. }
  • MyBook.java    用于增强 Book 类的 类, 给 Book 类添加更多功能
  1. package com.liuyanzhao.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class MyBook {
  4.     public void before1() {
  5.         System.out.println(“前置增强……….”);
  6.     }
  7.     //环绕通知
  8.     public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  9.         //方法之前
  10.         System.out.println(“方法之前….”);
  11.         //执行被增强的方法
  12.         proceedingJoinPoint.proceed();
  13.         //方法之后
  14.         System.out.println(“方法之后….”);
  15.     }
  16.     public void after1() {
  17.         System.out.println(“后置增强……….”);
  18.     }
  19. }
  • bean2.xml    这里我们使用的是文件配置,下次使用注解配置
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=http://www.springframework.org/schema/beans&#8221;
  3.                xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
  4.                xmlns:aop=http://www.springframework.org/schema/aop&#8221; xsi:schemaLocation=”
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd”> <!– bean definitions here –>
  7.     <!–1、配置对象–>
  8.     <bean id=“BookId” class=“com.liuyanzhao.aop.Book”></bean>
  9.     <bean id=“MyBookId” class=“com.liuyanzhao.aop.MyBook”></bean>
  10.     <!–2、配置 aop 操作–>
  11.     <aop:config>
  12.         <!–2.1、配置切入点–>
  13.         <aop:pointcut id=“pointcut1” expression=“execution(* com.liuyanzhao.aop.Book.add(..))” />
  14.         <!–2.2、配置切面–>
  15.         <aop:aspect ref=“MyBookId”>
  16.             <!–配置增强类型
  17.                 method:增强类里面使用哪个方法作为前置
  18.             —>
  19.             <aop:before method=“before1” pointcut-ref=“pointcut1”/>
  20.             <aop:after-returning method=“after1” pointcut-ref=“pointcut1”/>
  21.             <aop:around method=“around1” pointcut-ref=“pointcut1”/>
  22.         </aop:aspect>
  23.     </aop:config>
  24. </beans>
  • TestAop.java    测试类,检测 MyBook 类是否成功给 Book 类添加功能
  1. package com.liuyanzhao.aop;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class TestAop {
  6.     @Test
  7.     public void test() {
  8.         ApplicationContext context =
  9.             new ClassPathXmlApplicationContext(“bean2.xml”);
  10.         Book book = (Book) context.getBean(“BookId”);
  11.         book.add();
  12.     }
  13. }
  • 运行结果如下

前置增强……….

方法之前….

add………..

方法之后….

后置增强……….

说明,有效

 

Aspectj 的 aop 操作(注解方式)

 

代码如下

  • Book.java

同上

  • MyBook.java   增强 Book 类 中的 add 方法
  1. package com.liuyanzhao.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Before;
  7. @Aspect
  8. public class MyBook {
  9.     //在方法上使用注解来完成增强的动作
  10.     @Before(value = “execution(* com.liuyanzhao.aop.Book.add(..))”)
  11.     public void before1() {
  12.         System.out.println(“前置增强……..”);
  13.     }
  14.     //环绕通知
  15.     @Around(value = “execution(* com.liuyanzhao.aop.Book.add(..))”)
  16.     public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  17.         //方法之前
  18.         System.out.println(“方法之前….”);
  19.         //执行被增强的方法
  20.         proceedingJoinPoint.proceed();
  21.         //方法之后
  22.         System.out.println(“方法之后….”);
  23.     }
  24.     @After(value = “execution(* com.liuyanzhao.aop.Book.add(..))”)
  25.     public void after1() {
  26.         System.out.println(“后置增强……….”);
  27.     }
  28. }
  • bean2.xml
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=http://www.springframework.org/schema/beans&#8221;
  3.        xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
  4.        xmlns:aop=http://www.springframework.org/schema/aop&#8221; xsi:schemaLocation=”
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd”> <!– bean definitions here –>
  7.     <!–开启 aop 操作–>
  8.     <aop:aspectj-autoproxy> </aop:aspectj-autoproxy>
  9.     <!–创建对象–>
  10.     <bean id=“bookId” class=“com.liuyanzhao.aop.Book”></bean>
  11.     <bean id=“myBookId” class=“com.liuyanzhao.aop.MyBook”></bean>
  12. </beans>
  • TestAop.java  测试类

同上

  • 运行结果

同上

 

参考:传智播客视频


何处钟 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring 之 AOP 详解
喜欢 (0)
[15211539367@163.com]
分享 (0)

您必须 登录 才能发表评论!