spring(AOP通知)

切面

切面是封装通用业务逻辑的组件,可以作用到其他组件上。是spring组件中的某个方法、无返回类型、参数类型与通知类型有关。
一个切面 开启数据库 关闭数据库 开启事务 检查登录账号状态 监测账号权限

切点

用于指定哪些组件哪方法使用切面组件,Spring提供表达式来实现该制定。

通知

用于指定组件作用到目标组件的具体位置。

连接点(Joinpoint)
  增强程序执行的某个特定位置(要在哪个地方做增强操作)。Spring仅支持方法的连接点,既仅能在方法调用前,方法调用后,方法抛出异常时等这些程序执行点进行织入增强。

切点(Pointcut)
  切点是一组连接点的集合。AOP通过“切点”定位特定的连接点。通过数据库查询的概念来理解切点和连接点的关系再适合不过了:连接点相当于数据库中的记录,而切点相当于查询条件。

增强(Advice)
  增强是织入到目标类连接点上的一段程序代码。表示要在连接点上做的操作。

切面(Aspect)
  切面由切点和增强(引介)组成(可以包含多个切点和多个增强),它既包括了横切逻辑的定义,也包括了连接点的定义,SpringAOP就是负责实施切面的框架,它将切面所定义的横切逻辑织入到切面所指定的链接点中。

注解使用

@aspect 定义切面
@pointcut 定义切点
@before 标注Before Advice定义所在的方法
@afterreturning 标注After Returning Advice定义所在的方法
@afterthrowing 标注After Throwing Advice定义所在的方法
@after 标注 After(Finally) Advice定义所在的方法
@around 标注Around Advice定义所在的方法

1.AOP通知(配置xml文件)

QieMian1.java

public class QieMian1 {
public void qianzhi(JoinPoint jp) {
System.out.println("前置通知");
Object [] os = jp.getArgs();
for (Object object : os) {
System.out.println("参数列表为:"+object);
}
} public void houzhi(Object a) {
System.out.println("后置通知"+a);
} public void yichang(JoinPoint jp,Throwable ta) {
System.out.println("异常通知");
ta.printStackTrace();
} public void zuizhong() {
System.out.println("最终通知");
} public void huanrao(ProceedingJoinPoint pjp) {
try {
System.out.println("环绕前");
pjp.proceed();
System.out.println("环绕后");
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("环绕异常");
e.printStackTrace();
}finally {
System.out.println("环绕最终");
}
}
}

ApplicationContext.xml

 <!-- 切面1 -->
<bean id="qieMian1" class="com.zy.spring.tools.aop.QieMian1"></bean> <!-- 创建一个切面的bean -->
<aop:config> <!-- aop配置标签 -->
<aop:pointcut expression="execution(* com.zy.spring.service.*.*(..))" id="pc1"/> <!-- 创建一个切点 -->
<aop:aspect order="1" ref="qieMian1"><!-- 创建一个切面 order是切面创建的顺序 ref链接上面创建的切面bean -->
<aop:before method="qianzhi" pointcut-ref="pc1"/><!-- 前置通知 -->
<aop:after-returning method="houzhi" pointcut-ref="pc1" returning="a"/> <!-- 后置通知 returning返回参数-->
<aop:after-throwing method="yichang" pointcut-ref="pc1" throwing="ta"/> <!-- 异常通知 throwing返回参数-->
<aop:after method="zuizhong" pointcut-ref="pc1"/> <!-- 最终通知 -->
<aop:around method="huanrao" pointcut-ref="pc1"/> <!-- 环绕通知 -->
</aop:aspect>
</aop:config>

常用的@AspectJ形式Pointcut表达式的标志符:

1、execution:
Spring AOP仅支持方法执行类型的Joinpoint 所以execution将会是我们用的最多的标志符,用它来帮我们匹配拥有指定方法前面的Joinpoint。

匹配方法execution

execution(返回类型 类的路径.类名.函数名(参数类型1,参数类型2))

execution(String com.chinasofti.Target.save(String))

execution(* com.chinasofti.Target.save(String))

execution(* com.chinasofti.*.save(String))

execution(* com.chinasofti.*.*(String))

execution(* com..*.*(..))

2、Within:
Within标志符只接受类型声明,它将匹配指定类型下所有的Joinpoint。

匹配类within

匹配到类
<aop:pointcut id="targetPintcut" expression="within(com.chinasofti.Target)"/>
调用这个类中的任何一个方法,都会走通知

匹配到包下的类
<aop:pointcut id="targetPintcut" expression="within(com.chinasofti.*)"/>
调用这个包下的任何一个类中的方法,都会走通知

匹配到包下及子包下的类
<aop:pointcut id="targetPintcut" expression="within(com..*)"/>
调用com这个包下的,所有子包及其一个类中的方法,都会走通知

2.AOP通知(注解配置)

QieMian2.java

@Aspect//定义切面
@Component//注册bean
public class QieMian2 {
@Pointcut("execution(* com.zy.spring.service.*.*(..))") //标记切点规则
public void pointcut() {};//创建一个空的方法,相当于切入方法 @Before("pointcut()") //前置通知 切点就是上面创建的pointcut()方法
public void qianzhi(JoinPoint jp) {
System.out.println("注解前置通知");
Object [] os = jp.getArgs();
for (Object object : os) {
System.out.println("参数列表为:"+object);
}
}
@AfterReturning(pointcut="pointcut()",returning="a")//后置通知 returning是方法中定义的参数
public void houzhi(Object a) {
System.out.println("注解后置通知"+a);
}
@AfterThrowing(pointcut="pointcut()",throwing="ta")//异常通知 throwing是方法中定义的参数
public void yichang(JoinPoint jp,Throwable ta) {
System.out.println("注解异常通知");
ta.printStackTrace();
}
@After("pointcut()")//最终通知
public void zuizhong() {
System.out.println("注解最终通知");
}
@Around("pointcut()")//环绕通知
public void huanrao(ProceedingJoinPoint pjp) {
try {
System.out.println("注解环绕前");
pjp.proceed();//放行
System.out.println("注解环绕后");
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("注解环绕异常");
e.printStackTrace();
}finally {
System.out.println("注解环绕最终");
}
}
}

ApplicationContext.xml

       <!-- 注解扫描AOP -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

使用注解aop功能,加入aop1的注解扫描即可。

Spring基础——AOP通知的更多相关文章

  1. Spring学习笔记(二)Spring基础AOP、IOC

    Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...

  2. spring.net AOP通知类型

    上篇介绍了spring.net AOP的基本实现,其中有说到通知类型,首先在这里补充解释一下.最后出一个异常通知的实例,因为他的实现和别的通知有些不一样. 1.拦截环绕通知:在Spring中最基础的通 ...

  3. Java : Spring基础 AOP

    简单的JDK动态代理例子(JDK动态代理是用了接口实现的方式)(ICar是接口, GoogleCar是被代理对象, MyCC是处理方法的类): public class TestCar { publi ...

  4. Spring基础知识之基于注解的AOP

    背景概念: 1)横切关注点:散布在应用中多处的功能称为横切关注点 2)通知(Advice):切面完成的工作.通知定了了切面是什么及何时调用. 5中可以应用的通知: 前置通知(Before):在目标方法 ...

  5. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  6. Spring基础篇——Spring的AOP切面编程

    一  基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...

  7. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  8. Java基础-SSM之Spring的AOP编程

    Java基础-SSM之Spring的AOP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法.它是对OOP的 ...

  9. Spring 梳理 - AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)

    Spring  AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect)   1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是 ...

随机推荐

  1. 大型情感剧集Selenium:3_元素定位 #华为云·寻找黑马程序员#

    关于昨天的文章 今天有朋友反馈,代码运行的时候,selenium提示警告 DeprecationWarning: use options instead of chrome_options drive ...

  2. 【开发者portal在线开发插件系列一】profile和基本上下行消息

    前言: 开发者portal支持在线开发profile(即设备建模).在线开发插件.模拟应用管理设备.模拟设备上报数据接收命令.支持离线开发的profile和插件的上传部署,是合作伙伴快速集成设备.对接 ...

  3. MySQL必知必会(组合Where子句,Not和In操作符)

    SELECT prod_id, prod_price, prod_name FROM products ; SELECT prod_id, prod_price, prod_name FROM pro ...

  4. iOS textView的使用总结

    转自:http://blog.csdn.net/zhaopenghhhhhh/article/details/11597887 在.h文件中声明: @interface ProtocolViewCon ...

  5. javascript数据类型和类型转换

    一  数据类型 1)typeof 查看数据类型 1.number 数字 取值范围:正无穷 - 负无穷.NaN 正无穷:Number.POSITIVE_INFINITY 负无穷:Number.NEGAT ...

  6. SpringBoot使用freemarker模板

    导入依赖 <!-- 添加freemarker模版的依赖 --> <dependency> <groupId>org.springframework.boot< ...

  7. LightOj-1030 Discovering Gold (期望DP)

    You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave c ...

  8. 洛谷 题解 P2296 【寻找道路】

    Problem P2296 [寻找道路] solution 首先声明,这题我用了spfa,而: 关于spfa:它死了. 杀手: NOI 2018−T1 出题人 感谢出题人,没有卡spfa 用时: 20 ...

  9. python 金融应用(三)数据可视化

    matplotlib 库( http://www.matp1otlìb.org )的基本可视化功能. 主要是2-D绘图.金融绘图和3-D绘图 一.2-D绘图 1.1一维数据集 #导入所需要的包impo ...

  10. C#使用 OleDbConnection 连接读取Excel

    /// <summary> /// 读取Excel中数据 /// </summary> /// <param name="strExcelPath"& ...