(1)AOP概述

  - AOP:面向切面编程,扩展功能不修改源代码实现

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

(2)AOP底层原理

  原始方法-------》纵向继承体系

      

  横向机制:

JDK代理机制:jdk动态代理是由Java内部的反射机制来实现的.jdk动态代理的应用前提,必须是目标类基于统一的接口。如果没有上述前提,jdk动态代理不能应用。

CGlib代理机制:Cglib的原理是对指定的目标类动态生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类和final方法进行代理。

       

 

(3)AOP操作相关术语(重点切入点、增强、切面)

spring的AOP操作(基于aspectj的XML方式)

Joinpoint   连接点:类里面可以被增强的方法,这些方法称为连接点

Pointcut     切入点:在类里面可以有很多的方法被增强,但是在实际的操作中值只是增强了add()和update(),则这两个实际增强的方法称为切入点

Advice      通知/增强:实际增强的逻辑,称为增强,例如扩展日志的功能,这个日志功能称为增强

      分为:

(1)前置通知:在方法之前执行

     (2)后置通知:在方法之后执行

     (3)异常通知:方法出现异常

     (4)最终通知:在后置之后执行

     (5)环绕通知:在方法之前和方法之后都执行,例如获取执行时间

 Aspect      切面:把我们的Advice增强应用到具体的Pointcut切入点方法上面,这个过程称为切面

Introduction  引介 :是一种特殊的增强,它为类添加一些属性和方法。这样,即使一个业务类原本没有实现某个接口,通过AOP的引介功能,我们可以动态地为该业务类添加接口的实现逻辑,让业务类成为这个接口的实现类。

Target        目标对象增强逻辑的织入目标类。如果没有AOP,目标业务类需要自己实现所有逻辑,而在AOP的帮助下,目标业务类只实现那些非横切逻辑的程序逻辑,而性能监视和事务管理等这些横切逻辑则可以使用AOP动态织入到特定的连接点上。
Weaving   织入织入是将Advice增强添加对target目标类具体连接点上的过程。AOP像一台织布机,将目标类、增强或引介通过AOP这台织布机天衣无缝地编织到一起。根据不同的实现技术,AOP有三种织入的方式:
    a、编译期织入,这要求使用特殊的Java编译器。
    b、类装载期织入,这要求使用特殊的类装载器。
    c、动态代理织入,在运行期为目标类添加增强生成子类的方式。
    Spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。

proxy        代理:一个类被AOP织入增强后,就产出了一个结果类,它是融合了原类和增强逻辑的代理类。根据不同的代理方式,代理类既可能是和原类具有相同接口的类,也可能就是原类的子类,所以我们可以采用调用原类相同的方式调用代理类。

(4)Spring的AOP操作

1、在spring里面进行AOP操作,使用 AspecJ  实现

  AspectJ:面向切面的框架,它扩展了JAVA语言,是一个基于JAVA语言的AOP框架

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

  - (2)Spring2.0之后新增加了对 aspectj 支持

2、使用AspectJ 方式实现AOP操作有两种方式

  - (1)基于AspectJ 的xml配置

Book类:

  1. package cn.itcast.aop;
  2.  
  3. public class Book {
  4. public void add(){
  5. System.out.println("add book......");
  6. }
  7.  
  8. }

MyBook类:增强类对象

  1. package cn.itcast.aop;
  2.  
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4.  
  5. //增强类
  6. public class MyBook {
  7. public void befoer1(){
  8. System.out.println("前置before1......");
  9. }
  10. public void after1(){
  11. System.out.println("后置after1......");
  12. }
  13. //环绕通知
  14. public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
  15. //在方法之前执行
  16. System.out.println("方法之前执行.....");
  17.  
  18. //执行被增强的方法
  19. proceedingJoinPoint.proceed();
  20.  
  21. //在方法之后执行
  22. System.out.println("方法之后执行....");
  23. }
  24. }

AspectJ 的xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
  9.  
  10. <!--1、 配置对象 -->
  11. <bean id="book" class="cn.itcast.aop.Book"></bean>
  12. <bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
  13. <!-- 2、配置AOP操作 -->
  14. <aop:config>
  15. <!-- 2.1 配置切入点 -->
  16. <aop:pointcut expression="execution(* cn.itcast.aop.Book.add(..))" id="pointcut1"/>
  17. <!-- 2.2配置切面 :把增强用到方法上面 -->
  18. <aop:aspect ref="myBook">
  19. <!-- 配置增强类型 :method:增强的类里面,使用哪个方法作为前置增强-->
  20. <aop:before method="befoer1" pointcut-ref="pointcut1"/>
  21. <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
  22. <aop:around method="around1" pointcut-ref="pointcut1"/>
  23.  
  24. </aop:aspect>
  25. </aop:config>
  26. </beans>

测试代码:

  1. public static void main(String[] args) {
  2. ApplicationContext context=new ClassPathXmlApplicationContext("aopbeans.xml");
  3. Book book=(Book)context.getBean("book");
  4. book.add();
  5. }

结果:

前置before1......
方法之前执行.....
add book......
方法之后执行....
后置after1......

 - (2)基于aspectj 的注解方式

    - 第一步:在配置文件中设置对象

  1. <bean id="book" class="cn.sdust.aop.Book"></bean>
  2. <bean id="myBook" class="cn.sdust.aop.MyBook"></bean>

    - 第二步:开启aop操作

  1. <!-- 开启aop操作 -->
  2. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    - 第三步:在增强类上面使用注解完成AOP操作

Book类

  1. package cn.sdust.aop;
  2.  
  3. public class Book {
  4.  
  5. public void add(){
  6. System.out.println("add book.....");
  7. }
  8. }

MyBook类

  1. package cn.sdust.aop;
  2.  
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.After;
  5. import org.aspectj.lang.annotation.AfterReturning;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Before;
  9.  
  10. @Aspect
  11. public class MyBook {
  12. //在方法上面使用注解完成增强配置
  13. @Before(value="execution(* cn.sdust.aop.Book.*(..))")
  14. public void before(){
  15. System.out.println("before......");
  16. }
  17. @AfterReturning(value="execution(* cn.sdust.aop.Book.*(..))")
  18. public void after(){
  19. System.out.println("after......");
  20. }
  21. @Around(value="execution(* cn.sdust.aop.Book.*(..))")
  22. public void round(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
  23. System.out.println("round--before....");
  24. proceedingJoinPoint.proceed();
  25. System.out.println("round--after....");
  26. }
  27. }

xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop" 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.  
  8. <!-- 开启aop操作 -->
  9. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  10. <!-- 创建对象 -->
  11. <bean id="book" class="cn.sdust.aop.Book"></bean>
  12. <bean id="myBook" class="cn.sdust.aop.MyBook"></bean>
  13. </beans>

测试代码

  1. @Test
  2. public void testaop(){
  3. ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
  4. Book book=(Book) context.getBean("book");
  5. book.add();
  6. }

运行结果

round--before....
before......
add book.....
round--after....
after......

AOP 操作准备:

1、jar包

  - 基本jar包

  

  - aspectj 的jar 包

  

 2、xml中的约束有beans、context、aop

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
  9.  
  10. </beans>

使用表达式配置切入点

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

  2、常用表达式

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

  (1)execution (* com.skd.aop.Book.add(..))//其中的(1)*表示修饰符(2)..表示如果有参数,也包括

  (2)execution (* com.skd.aop.Book.*(..)) //Book/类的所有方法

  (3)execution (* *.*(..))//所有类的所有方法

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

Spring 学习(三)AOP的更多相关文章

  1. Spring学习之AOP的实现方式

    Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...

  2. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  3. spring学习(二) ———— AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  4. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  5. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  6. spring学习三:Spring的Aop、代理

    ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语:    Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...

  7. Spring学习之Aop的基本概念

    转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...

  8. Spring学习之AOP与事务

      一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...

  9. Spring学习三

    Spring注解来注入bean 在classpath中扫描组件 组件扫描,即componetscanning 利用注解来扫描的组件有  @Component  :基本注解,表示一个受Spring管理的 ...

  10. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

随机推荐

  1. android开发 服务器端访问MySQL数据库,并把数据库中的某张表解析成xml格式输出到浏览器

    我们此时只要写一个Servlet就可以了: public class UpdateMenuServlet extends HttpServlet { /** * */ private static f ...

  2. EXPLAIN 命令

    MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提 ...

  3. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  4. JVM体系结构之五:本地方法栈

    对于一个运行中的Java程序而言,它还可能会用到一些跟本地方法相关的数据区.当某个线程调用一个本地方法时,它就进入了一个全新的并且不再受虚拟机限制的世界.本地方法可以通过本地方法接口来访问虚拟机的运行 ...

  5. 数组排序----Demo

    //选择排序,分为简单选择排序.树形选择排序(锦标赛排序).堆排序 此算法为简单选择排序 public static void selectSort(int[] a){ for(int i=0;i&l ...

  6. WPF实现右键菜单

    ContextMenu类就是用来做右键菜单的对象,对于任何的控件都可以进行对ContextMenu属性的操作进行设置右键菜单的功能. 下面代码就是对一个按钮添加一个WPF右键菜单的功能: < B ...

  7. <正则吃饺子> :关于mybatis中使用的问题(一)

    在公司项目开始之前,根据springboot .mybatis.Swagger2 整合了一个demo,在测试时候,遇到的问题,简单记录.之前在使用mybatis时候,没有注意到这一点. 1.错误:Th ...

  8. 使用gradle上传项目到jcenter

    想不想把自己的库也上传到jcenter,然后只需要一名话 compile com.zzb.library:android-common:0.1.0 //(compile group_id:artifa ...

  9. React组件详细介绍及其生命周期函数

    组件的详细说明 通过Reac.createClass({...})创建组件的时候,应该有一个render()方法,也可以在其中添加生命周期函数. render方法 当调用该方法的时候,会检测this. ...

  10. [ural1132]Square Root(cipolla算法)

    题意:求${x^2} \equiv n\bmod p$ 解题关键: 定理:若$a$满足$w = {a^2} - n$是模$p$的二次非剩余,即,${x^2} = w\bmod p$无解,则${(a + ...