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. blktrace分析IO

    http://bean-li.github.io/blktrace-to-report/ 前言 上篇博客介绍了iostat的一些输出,这篇介绍blktrace这个神器.上一节介绍iostat的时候,我 ...

  2. Solr学习总结(六)solr的函数查询Function Queries

    摘要: 函数查询允许你使用一个或多个数字字段的真实值生成一个相关性分数,函数查询在standard,DisMax,eDisMax下都能使用. 查询函数可以是常量,字段或者其他函数的组合.使用函数可以影 ...

  3. Python3 引入sqlite3时出现错误:ModuleNotFoundError: No module named '_sqlite3'

    在Python3 中内置了SQLite3,但是在编译安装完之后执行: import sqlite3 出现错误: ModuleNotFoundError: No module named '_sqlit ...

  4. 网络层-IP地址

    以下内容是IPv4 IP地址长度32位,Java里面一个int的长度,总共分为5类IP地址 1:分类编址 A类IP地址0开头:           A类有31个位置可以变化,总数是2^31个, [(0 ...

  5. vue pm2守护进程

    Linux 创建一个.sh可执行脚本,例如hexo.sh 代码 12 #!/usr/bin/env bashhexo server 使用pm2 start hexo.sh执行脚本 Windows 创建 ...

  6. 使MySQL查询区分大小写的实现方法

    发布:mdxy-dxy 字体:[增加 减小] 类型:转载 我们在MySQL中使用SELECT语句查询时,可不可以使查询区分大小写?今天从网络上找到了方法,现总结如下.   1.一种方法是可以设置表或行 ...

  7. WinRAR命令行版本 rar.exe使用详解

    RAR 命令行语法~~~~~~~~~~~~~~ 语法 RAR.exe <命令>  [ -<开关> ]  <压缩文件>  [ <@列表文件...> ]   ...

  8. Spring MVC 异常处理 - ResponseStatusExceptionResolver

    作用在类和方法上面 更改返回的代码和错误消息 类上 通过throw new UserName***Exception()抛出 @ResponseStatus(value=HttpStatus.FORB ...

  9. python中关键字的总结

    python中各种关键字的总结:用表格形式,解释关键字符号的作用和案例说明                    关键字                                         ...

  10. 19 网络编程--Socket 套接字方法

    1.Socket(也称套接字)介绍 socket这个东东干的事情,就是帮你把tcp/ip协议层的各种数据封装啦.数据发送.接收等通过代码已经给你封装好了 ,你只需要调用几行代码,就可以给别的机器发消息 ...