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. 第12章 网络基础(1)_网络分层和TCP/IP协议族

    1. 协议的概念 (1)计算机网络中实现通信必须有一些约定.如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被称为通信协议 (2)在两个节点之间要成功地进行通信,两个节点之间必须 ...

  2. Apache Doris通过supervisor进行进程管理

    下面一段文字是摘自doris官方文档:注:在生产环境中,所有实例都应使用守护进程启动,以保证进程退出后,会被自动拉起,如 Supervisor.如需使用守护进程启动,需要修改各个 start_xx.s ...

  3. Spark分析之Master、Worker以及Application三者之间如何建立连接

    Master.preStart(){ webUi.bind() context.system.scheduler.schedule( millis, WORKER_TIMEOUT millis, se ...

  4. 知识picture

  5. chrome浏览器控制台 console不打印信息问题解决办法。

    转自:https://blog.csdn.net/wang17866603359/article/details/79083776 最近换了安装chrome,想按F12调试下代码,发现控制台什么信息都 ...

  6. J2SE 8的Lambda --- 语法

    语法例子 LambdaGrammarTest lambdaTest = new LambdaGrammarTest(); // 1. 能够推导出类型的,可以不写类型 String[] planets ...

  7. Oracle SQL Developer在进行查询的时候只显示50条数据

    在查询结果大于50条的时候,软件默认会只显示50条,向下拉会继续显示. 想要显示所有结果的话,光标放在结果集:ctrl+End或者是ctrl+PgDn都可以.

  8. Haskell语言学习笔记(54)Data.Set

    Data.Set Prelude> import Data.Set as Set Prelude Set> :set -XOverloadedLists Construction Prel ...

  9. Haskell语言学习笔记(35)Contravariant

    contravariant 模块 contravariant 模块需要安装 $ cabal install contravariant contravariant-1.4 Prelude> :m ...

  10. Session的作用和使用场景

    1.session何时被创建? 客户首次访问服务器时,回话session对象被创建并分配一个唯一的Id,同时id号发送到客户端,并存入cookie,使得客户端session对象和服务器端一致. 2.如 ...