文章大纲

一、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. Alibaba(阿里) RocketMQ入门实例

    摘自:码友18年(www.mayou18.com) what is rocketMQ? RocketMQ作为一款分布式的消息中间件(阿里的说法是不遵循任何规范的,所以不能完全用JMS的那一套东西来看它 ...

  2. 20.QT-Qpixmap实现图片鼠标缩放,鼠标拖动示例(详解)

    通过 QPainter 绘画实现,以本地图片985*740为例 如下图所示: 效果如下所示: 实现原理 主要通过以下函数实现: , ); //平铺显示pixmap //x y w h :表示绘画区域 ...

  3. 居中 html css

    <div id="main" style="width:800px;height: 600px;margin:0 auto"> <!-- 这里 ...

  4. LoadRunner Mysql性能优化

    原文:https://blog.csdn.net/u011910905/article/details/49863787 一.查询与索引优化分析 1.show命令: show engines; 显示存 ...

  5. checkbox事件的变化

    <input type="checkbox" checked={this.state.checked} onChange={this.checkedChangeHandler ...

  6. 一个靠谱的phpredisadmin文件

    http://download.csdn.net/detail/newjueqi/7227487

  7. bash: jar: 未找到命令..(command not found)

    /bin/bash: jar: command not found 解决办法: cd /usr/bin 必须先进入/usr/bin,下同 sudo ln -s -f /usr/lib/jvm/jdk1 ...

  8. vue的传参方式和router使用技巧

    vue传参方法一 1,路由配置 { path: '/describe/:id', name: 'Describe', component: Describe } 2,使用方法 // 直接调用$rout ...

  9. .net core使用Ocelot+Identity Server统一网关验证

    源码下载地址:下载 项目结构如下图: 在Identity Server授权中,实现IResourceOwnerPasswordValidator接口: public class IdentityVal ...

  10. BZOJ_4517_[Sdoi2016]排列计数_组合数学

    BZOJ_4517_[Sdoi2016]排列计数_组合数学 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[ ...