文章大纲

一、AOP介绍
二、Spring的AOP实战
三、AOP常用标签
四、项目源码及参考资料下载
五、参考文章

 

一、AOP介绍

1. 什么是AOP

  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

  简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强。

2. AOP的作用及优势

  作用:在程序运行期间,不修改源码对已有方法进行增强。
  优势:减少重复代码 提高开发效率 维护方便

3. AOP的实现方式

  使用动态代理技术

二、Spring的AOP实战

1. 相关术语介绍

(1)Joinpoint(连接点):所谓连接点是指那些被拦截到的点,spring中,这些点是指方法,因为spring只支持方法类型的连接点
(2)PointCut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义
(3)Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知,通知分为迁址通知、后置通知、异常通知、最终通知、环绕通知(切面要完成的功能)
  前置通知:在方法之前执行
  后置通知:在方法之后执行
  异常通知:方法出现异常
  最终通知:在后置之后执行
  环绕通知:在方法之前和之后都执行
(4)Aspect(切面):是切入点和通知(引介)的结合,把增强应用到具体方法上面,过程就叫切面。也就是把增强用到切入点的过程

2. 实现方式

(1)基于aspectJ的xml配置
(2)基于aspectJ的注解方式

3. 基于aspectJ的xml配置代码实战

创建测试类Book.java

package aop;

public class Book {

    public void add() {

        System.out.println("add.......");

    }

}

创建增强、通知类MyBook.java

package aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

    public void before1() {

        System.out.println("前置增强...");

    }

    public void after1() {

        System.out.println("后置增强...");

    }

    //环绕通知
public void around1(ProceedingJoinPoint proceedingJoinPoint) { //方法之前
System.out.println("方法之前..."); //执行被增强方法
try { proceedingJoinPoint.proceed(); } catch (Throwable e) { e.printStackTrace();
} //方法之后
System.out.println("方法之后...");
} }

src下配置文件bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 配置两个类的对象 -->
<bean id="book" class="aop.Book"></bean>
<bean id="myBook" class="aop.MyBook"></bean> <!-- 配置aop操作 -->
<aop:config>
<!-- 配置切入点 expression就是写表达式的,id就是切入点名字 -->
<aop:pointcut expression="execution(* aop.Book.*(..))" id="pointcut1"/> <!-- 配置切面 把增强用到方法上面 ref是增强类的对象 -->
<aop:aspect ref="myBook"> <!-- 配置前置增强 method表示增强类里面使用哪个方法作为前置 pointcut-ref表示把增强方法配置到哪个切入点-->
<aop:before method="before1" pointcut-ref="pointcut1"/> <!-- 配置后置增强 -->
<aop:after method="after1" pointcut-ref="pointcut1"/> <!-- 配置环绕增强 -->
<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>

测试代码如下

package aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test
public void testUser() { //加载spring配置文件,根据内容创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Book book = (Book) context.getBean("book"); book.add(); } }

运行结果如下

 

4. 基于aspectJ的注解方式代码实战

创建测试类Book.java
  代码与xml配置中一样

创建增强、通知类MyBook.java

package aop2;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; //Spring就能发现用@Aspect注解的切面内并把它应用到目标对象上。
@Aspect
public class MyBook { //在方法上面使用注解完成增强配置
@Before(value="execution(* aop2.Book.*(..))")
public void before1() { System.out.println("前置增强..."); } @After(value="execution(* aop2.Book.*(..))")
public void after1() { System.out.println("后置增强..."); } @Around(value="execution(* aop2.Book.*(..))")
//环绕通知
public void around1(ProceedingJoinPoint proceedingJoinPoint) { //方法之前
System.out.println("方法之前..."); //执行被增强方法
try { proceedingJoinPoint.proceed(); } catch (Throwable e) { e.printStackTrace();
} //方法之后
System.out.println("方法之后...");
} }

src下配置文件bean2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 开启aop操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 创建对象 -->
<bean id="book" class="aop2.Book"></bean>
<bean id="myBook" class="aop2.MyBook"></bean> </beans>

测试代码如下

package aop2;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test
public void testUser() { //加载spring配置文件,根据内容创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); Book book = (Book) context.getBean("book"); book.add(); } }

运行结果如下

 

三、AOP常用标签

(1)<aop:config>
  用于声明开始aop的配置
(2)<aop:aspect>
  作用:用于配置切面。
  属性:
  id:给切面提供一个唯一标识。
  ref:引用配置好的通知类bean的id。
(3)<aop:pointcut>
  作用:用于配置切入点表达式
  属性:
  expression:用于定义切入点表达式。
  id:用于给切入点表达式提供一个唯一标识。
(4)<aop:before>
作用:用于配置前置通知
  属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
(5)<aop:after-returning>
  作用:用于配置后置通知
  属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
(6)<aop:after-throwing>
  作用:用于配置异常通知
  属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
(7)<aop:after>
  作用:用于配置最终通知
  属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
(8)<aop:around>
  作用:用于配置环绕通知
  属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用

四、项目源码及参考资料下载

链接:https://pan.baidu.com/s/1mU6ktTrwggh9SVmsoucJRg
提取码:jep8

五、参考文章

http://yun.itheima.com/course/215.html?1804lckj

Spring之AOP详解的更多相关文章

  1. Spring、AOP详解

    如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...

  2. 3、Spring的AOP详解和案例

    AOP(Aspect Oriented Programming),即面向切面编程. 1.OOP回顾 在介绍AOP之前先来回顾一下大家都比较熟悉的OOP(Object Oriented Programm ...

  3. spring的aop详解

    一.aop术语 1.连接点joinpoint: 程序执行的某个特定位置:如类开始初始化之前.类初始化之后.类某个方法调用前.调用后等.Spring仅支持方法的连接点,即仅能在方法调用前.方法调用后以及 ...

  4. (三)Spring 之AOP 详解

    第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...

  5. 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理

    Spring AOP详解 . JDK动态代理.CGLib动态代理  原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...

  6. [Spring学习笔记 5 ] Spring AOP 详解1

    知识点回顾:一.IOC容器---DI依赖注入:setter注入(属性注入)/构造子注入/字段注入(注解 )/接口注入 out Spring IOC容器的使用: A.完全使用XML文件来配置容器所要管理 ...

  7. Spring AOP详解(转载)所需要的包

    上一篇文章中,<Spring Aop详解(转载)>里的代码都可以运行,只是包比较多,中间缺少了几个相应的包,根据报错,几经百度搜索,终于补全了所有包. 截图如下: 在主测试类里面,有人怀疑 ...

  8. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  9. 转:Spring AOP详解

    转:Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

随机推荐

  1. SpringCloud实战-Hystrix线程隔离&请求缓存&请求合并

    接着上一篇的Hystrix进行进一步了解. 当系统用户不断增长时,每个微服务需要承受的并发压力也越来越大,在分布式环境中,通常压力来自对依赖服务的调用,因为亲戚依赖服务的资源需要通过通信来实现,这样的 ...

  2. PAT1064: Compelte Binary Search Tree

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  3. mysql SQL Layer各个模块介绍

    https://blog.csdn.net/cymm_liu/article/details/45821929

  4. 最值得收藏的java技术博客(Java篇)

    第一个:java_my_life 作者介绍:找不到原作者信息.大概做了翻阅全部是2012年的博客. 博客主要内容:主要内容是关于Java设计模式的一些讲解和学习笔记,在相信对学习设计模式的同学帮助很大 ...

  5. When to use next() and return next() in Node.js

    Some people always write return next() is to ensure that the execution stops after triggering the ca ...

  6. Android开发——子进程更新UI

    方式一:Handler和Message ① 实例化一个Handler并重写handlerMessage()方法 private Handler handler = newHandler() { pub ...

  7. 巩固java(四)----super和super()

    引言: 一个公司里有普通员工和经理,他们之间有很多共同点,但也有一些差异,比如薪水问题,普通员工只有普通工资,经理在完成绩效后有一定比例的奖金.这时我们可以定义两个类Employee和Manager, ...

  8. infolite(中文检索系统)~爬虫利器

    infolite 今天为大家分享一个爬虫利器-infolite.这是一个chrome浏览器的插件,如果你在写爬虫的时候对复杂繁琐的控件路径分析是深恶痛绝.那么infolite绝对是你最好的选择. 安装 ...

  9. python 文件的写删改

    # coding=utf-8 # !/usr/bin/python # -*- coding: UTF-8 -*- import io import os def file_chance(): #修改 ...

  10. Python 可视化工具 Matplotlib

    英文出处:Chris Moffitt. Matplotlib是Python中最常用的可视化工具之一,可以非常方便地创建海量类型的2D图表和一些基本的3D图表.本文主要介绍了在学习Matplotlib时 ...