AspectJ的XML方式完成AOP的开发之AOP的通知类型
1. 前置通知
* 在目标类的方法执行之前执行。
* 配置文件信息:<aop:after method="before" pointcut-ref="myPointcut3"/>
* 应用:可以对方法的参数来做校验
2. 最终通知
* 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
* 在配置文件中编写具体的配置:<aop:after method="after" pointcut-ref="myPointcut3"/>
* 应用:例如像释放资源
3. 后置通知
* 方法正常执行后的通知。
* 在配置文件中编写具体的配置:<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>
* 应用:可以修改方法的返回值
4. 异常抛出通知
* 在抛出异常后通知
* 在配置文件中编写具体的配置:<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/>
* 应用:包装异常的信息
5. 环绕通知
* 方法的执行前后执行。
* 在配置文件中编写具体的配置:<aop:around method="around" pointcut-ref="myPointcut2"/>
* 要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。
代码举例:
(1)创建接口CustomerDao:
package com.huida.demo3;
public interface CustomerDao {
public void save();
public void update();
}
(2)创建实现接口的CustomerDaoImpl类:
package com.huida.demo3;
public class CustomerDaoImpl implements CustomerDao{
@Override
public void save() {
//int i=1/0;
System.out.println("添加客户");
}
@Override
public void update() {
System.out.println("更新客户");
}
}
(3)创建切面类
package com.huida.demo3;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspectXml {
//前置通知:方法在目标对象方法之前执行,这里目标方法为save方法
public void log(){
System.out.println("添加日志");
}
//最终通知:不管出不出现异常,都会执行
public void after(){
System.out.println("最终通知执行");
}
/*
* 后置通知:方法执行之后,执行后置通知,程序出现异常,后置通知不会执行
*/
public void afterReturn(){
System.out.println("后置通知");
}
/*
* 异常通知:出现异常之后执行
*/
/*
* 环绕通知
*/
public void around(ProceedingJoinPoint point){
System.out.println("环绕通知1");
//手动的让目标对象的方法执行,这里的目标对象方法为save()
try {
point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("环绕通知2");
}
}
(4)配置文件:
<?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 -->
<!-- 配置客户dao -->
<bean id="customerDao" class="com.huida.demo3.CustomerDaoImpl"></bean>
<!-- 配置切面类 -->
<bean id="myAspectXml" class="com.huida.demo3.MyAspectXml"></bean>
<!-- 配置AOP -->
<aop:config>
<!--切面类 -->
<aop:aspect ref="myAspectXml">
<!-- 是在原始方法的前面执行,还是后面执行 -->
<!-- pointcut:切入点 -->
<!-- 切入点表达式
1.execution()固定的 不能不写
2.public可以省略不写,但如果是private则必须要写
3.void 返回值可以出现*表示任意的返回值 。返回值类型不能不写
4.可以使用*代替, 必须编写。如果想找这个项目中所有的方法,不能只写一个*,而应写为*..*
5.类名 可以写为*DaoImpl,表示拦截以DaoImpl的方法
6.方法名 save* 拦截方法以save开头的方法。
7.方法的参数
-->
<!-- <aop:before method="log" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> -->
<!-- <aop:after method="after" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> -->
<!-- <aop:after-returning method="afterReturn" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> -->
<aop:around method="around" pointcut="execution(public void com.huida.demo3.CustomerDaoImpl.save*(..))"/> </aop:aspect>
</aop:config> </beans>
(5)测试代码为:
package com.huida.demo3; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class demo3 { @Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void run1(){
customerDao.save();
customerDao.update();
}
}
(6)执行结果:
a.前置通知的执行结果:

b.最终通知的执行结果:

c.后置通知的执行结果:

d.环绕通知的执行结果:

AspectJ的XML方式完成AOP的开发之AOP的通知类型的更多相关文章
- Spring基于AspectJ的AOP的开发之AOP的相关术语
1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...
- 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)
[AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...
- 基于AspectJ的XML方式进行AOP开发
-------------------siwuxie095 基于 AspectJ 的 XML 方式进行 AOP 开发 1 ...
- Java开发学习(十六)----AOP切入点表达式及五种通知类型解析
一.AOP切入点表达式 对于AOP中切入点表达式,总共有三个大的方面,分别是语法格式.通配符和书写技巧. 1.1 语法格式 首先我们先要明确两个概念: 切入点:要进行增强的方法 切入点表达式:要进行增 ...
- spring框架之AspectJ的XML方式完成AOP的开发
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- Spring的AOP开发(基于AspectJ的XML方式)
Spring的AOP的简介: AOP思想最早是由AOP联盟组织提出的.Spring是使用这种思想最好的框架 Spring的AOP有自己实现的方式(非常繁琐). Aspect是一个AOP的框架, Spr ...
- Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)
参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...
- AspectJ的XML方式完成AOP的开发之切入点的表达式
1. 再配置切入点的时候,需要定义表达式,重点的格式如下:execution(public * *(..)),具体展开如下: * 切入点表达式的格式如下: * execution([修饰符] 返回值类 ...
- Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)
1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...
随机推荐
- 代码生成器 CodeSmith 的使用(二)
在第一篇中,简单的介绍了 CodeSmith 的使用方法,这次做一个生成简单的数据库字段属性的模板.以下只粘贴主要的代码片段. <%-- Name: Copyright © Sun 2013-2 ...
- python之集合操作
1.集合的增操作 add(item):增加至集合中 update(set): 若不存在,更新至集合中 2.集合的删操作(五种) pop(): 随机删除并返回值 remove(item): 删除va ...
- javascript 常用获取页面宽高信息 API
在页面的构建中 常常会需要获取页面的一些宽高信息,例如实现 惰性加载图片 需要获取页面的可见区域高度 和 已滚动区域的高度,以判断图片所在位置是否可见来决定加载图片的时间, 花点时间整理了一下,获取页 ...
- Nginx-ingress-controller部署
参考官网https://kubernetes.github.io/ingress-nginx/ 部署pod:nginx-ingress-controller/nginx-default-backend ...
- PHP把excel导入mysql数据库最常用的方法
Posted on 2011-03-25 09:16 PHP博客 阅读(1316) 评论(0) 编辑 收藏 引用 网摘 PHP把excel(xls)文件导入mysql数据库最常用的方法就是先把xls ...
- mvc框架路由原理
到目前为止已经使用过很多php框架,比如:Zendframework,ThinkPHP,YII,Slim.但还未静下心来研究过框架的原理. 今天首先来看一下mvc框架中路由的原理: 所谓路由,就是程序 ...
- 字符串和JSON对象互转的方法
采用Ajax的项目开发过程中,经常需要将JSON格式的字符串返回到前端,前端解析成JS对象(JSON ).字符串转JSON对象 1.eval方式解析.function strToJson(str){ ...
- ABAP-BarCode-3-调用第三方控件BarTender实现打印
1.BarTender软件安装及注册 2.BarTender设置好打印模板 3.ABAP生成TXT文件放置FTP服务器指定文件夹 4.BarTender轮询FTP服务器文件夹中的TXT,并按照模板打印 ...
- Graylog安装配置
ES集群健康检测:curl -sXGET http://localhost:9200/_cluster/health?pretty=true | grep "status" | a ...
- 一步步实现 easyui datagrid表格宽度自适应,效果非常好
一步步实现 easyui datagrid表格宽度自适应,效果非常好: 一.设置公共方法,使得datagrid的属性 fitColumns:true $(function(){ //初始加载,表格宽 ...