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的通知类型的更多相关文章

  1. Spring基于AspectJ的AOP的开发之AOP的相关术语

    1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...

  2. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  3. 基于AspectJ的XML方式进行AOP开发

    -------------------siwuxie095                                 基于 AspectJ 的 XML 方式进行 AOP 开发         1 ...

  4. Java开发学习(十六)----AOP切入点表达式及五种通知类型解析

    一.AOP切入点表达式 对于AOP中切入点表达式,总共有三个大的方面,分别是语法格式.通配符和书写技巧. 1.1 语法格式 首先我们先要明确两个概念: 切入点:要进行增强的方法 切入点表达式:要进行增 ...

  5. spring框架之AspectJ的XML方式完成AOP的开发

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  6. Spring的AOP开发(基于AspectJ的XML方式)

    Spring的AOP的简介: AOP思想最早是由AOP联盟组织提出的.Spring是使用这种思想最好的框架 Spring的AOP有自己实现的方式(非常繁琐). Aspect是一个AOP的框架, Spr ...

  7. Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)

    参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...

  8. AspectJ的XML方式完成AOP的开发之切入点的表达式

    1. 再配置切入点的时候,需要定义表达式,重点的格式如下:execution(public * *(..)),具体展开如下: * 切入点表达式的格式如下: * execution([修饰符] 返回值类 ...

  9. Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

    1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...

随机推荐

  1. javascript继承之借用构造函数(二)

    //简单的函数调用 function Father() { this.nums= [1,2]; } function Son() { Father.call(this);//调用超类型,完成son继承 ...

  2. EMNLP 2018 | 用强化学习做神经机器翻译:中山大学&MSRA填补多项空白

    人工深度学习和神经网络已经为机器翻译带来了突破性的进展,强化学习也已经在游戏等领域取得了里程碑突破.中山大学数据科学与计算机学院和微软研究院的一项研究探索了强化学习在神经机器翻译领域的应用,相关论文已 ...

  3. 序列化模块json--pickle--shelve

    什么是序列化? 将一组或多组数据结构转化成一个字符串的过程就叫做序列化 它的目的: 序列化的结构是字符串,准确的说是bytes类型,方便存储 方便于网络传输, 既然序列化是从数据类型到字符串的过程,那 ...

  4. python中键值叫唤例子

    >>> myDict = {'a':'A','b':'B','c':'C'} >>> myDict {'a': 'A', 'c': 'C', 'b': 'B'} & ...

  5. JS - 函数,Math,number

    函数分为:关键字function 匿名函数 — 没有名字的函数 有名函数 — 有名字的函数 <body> <div></div> <script> // ...

  6. c++builder XE6 线程 tthread

    thread TThread class TSleepFunc : public TCppInterfacedObject<TProc> { public: TSleepFunc(TFor ...

  7. ABAP-Generate subroutine

    1.定义 data:zprog like abapsource occurs with header line, prog() type c, msg() type c. 2.动态语句 zprog-l ...

  8. LINQ to SQL语句(1)Select查询的九种形式

    目录 说明 简单形式 匿名类型形式 条件形式 指定类型形式 筛选形式 Shaped形式 嵌套形式 本地调用方法形式 Distinct形式 说明 与SQL命令中的select作用相似但位置不同,查询表达 ...

  9. heat 用法 示例

    heat.exe dir "./SampleFolder" -cg Files -dr INSTALLDIR -gg -g1 -sf -srd -var "var.Tar ...

  10. Gearman安装及使用

    基础安装包 yum install vim wget gcc gcc-c++ make dos2unix gperf libevent libevent-devel zlib-devel bzip2- ...