springAOP的各种通知
来源:互联网 发布:apache cgi区别 编辑:程序博客网 时间:2024/06/05 16:07
切面及切面的目标方法
package com.mo.transaction;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;public class Transaction {/** * 前置通知 * 在目标方法执行之前执行 * 参数:连接点,可以获取连接点相关的信息 */public void beginTransaction(JoinPoint joinPoint){String name = joinPoint.getSignature().getName();System.out.println("连接点的名称" + name);//客户端调用那个方法,那个方法就是连接点System.out.println("目标类" + joinPoint.getTarget().getClass());//通过连接点可以获取目标类System.out.println("beginTransaction");}/** * 后置通知 * 在目标方法执行之后执行 * 可以获取目标方法的返回值:通过在后置通知中配置的returning="val",在通知这里需要配置参数Object val,用来收集参数 * 注意:当调用连接点的方法的时候,抛出了异常,那么后置通知将不再执行 */public void commit(JoinPoint joinPoint,Object val){//这里的参数,val要与spring配置文件中的后置通知中的returning="val" 一致System.out.println(val);System.out.println("commit");}/** * 最终通知 */public void finallyMethod(){System.out.println("finallyMethod");}/** * 异常通知 */public void throwingMehtod(JoinPoint joinPoint,Throwable ex){System.out.println(ex.getMessage());System.out.println("throwingMehtod");}/** * 这是一个环绕通知 * joinPoint.proceed(); 这行代码代表执行目标方法,不写就是不执行目标方法 */public void aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("qqq");joinPoint.proceed();//这里是调用代码}}
applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="personDao" class="com.mo.transaction.PersonDaoImpl"></bean> <bean id="transaction" class="com.mo.transaction.Transaction"></bean><aop:config><!-- 切入点表达式,确定目标类 --><aop:pointcut expression="execution(* com.mo.transaction.PersonDaoImpl.*(..))" id="perform"/><!-- ref指向的对象就是切面 --><aop:aspect ref="transaction"><!-- 前置通知,指向切入点表达式1.在目标方法执行之前2.获取不到目标方法的返回值 --><aop:before method="beginTransaction" pointcut-ref="perform"/><!-- 后置通知1.后置通知可以获取到目标方法的返回值注意:这里设定的val是接受目标方法的返回值的,在后置通知中接受返回值 参数的名称也是val,要与这里定义的一致2.当目标方法抛出异常,后置通知将不再执行 --> <aop:after-returning method="commit" pointcut-ref="perform" returning="val"/> <!-- 最终通知 无论目标方法是否抛出异常都将执行 --> <aop:after method="finallyMethod" pointcut-ref="perform"/> <!-- 异常通知 获取目标方法抛出异常的信息 --> <aop:after-throwing method="throwingMehtod" pointcut-ref="perform" throwing="ex"/> <!-- 环绕通知 能够控制目标方法的执行 前置通知和后置通知能在目标方法的前面和后面加一些代码,但是不能控制目标方法的执行 注意:环绕通知中参数中的连接点是用ProceedingJoinPoint,是JoinPoint的一个子类 --> <aop:around method="aroundMethod" pointcut-ref="perform"/></aop:aspect></aop:config></beans>
1 0
- springAOP的各种通知
- springAOP中的各种通知
- SpringAOP环绕通知的使用
- SpringAOP:环绕通知Around
- SpringAOP的五种通知的配置及使用
- springAOP基于xml的配置前后环绕通知
- spring的各种通知
- SpringAOP 通知(adivce)- methodIntercepor
- SpringAOP处理通知中的参数
- springaop术语概述及aop中常见通知的实现(前置,后置,环绕,异常)
- MPMoviePlayerController的播放状态的各种通知
- Spring中常见的各种通知
- 通知的各种实用写法技巧
- AspectJ 基于注解的实现各种通知
- SpringAOP的实现
- 基于注解的SpringAOP
- SpringAop的使用
- SpringAop的相关概念
- Android Things:用户驱动-传感器
- 应用统计学与R语言实现学习笔记(一)——简介
- 经典例题(时钟)
- windows几个网络相关命令
- leetcode 72 Edit Distance(DP)
- springAOP的各种通知
- 易聚之X元素
- 文本处理工具
- MYSQL 触发器的高级使用
- 响应式实例-兰途科技(jq)
- 易聚之外部设备
- 从零开始学贪心算法
- java继承简例
- tab栏切换(点击图片切换路径)