博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring Aop2
阅读量:4697 次
发布时间:2019-06-09

本文共 5676 字,大约阅读时间需要 18 分钟。

  昨天记录了Spring AOP学习的一部分(),本来是想一口气梳理完的。但是大晚上时间不够(无奈一场奥运篮球总决赛耗费掉了2小时,不过的确相当精彩),又考虑到篇幅太长,阅读性比较差,所以将后半部分更偏于应用的重起一篇随笔。

  利用方式一的配置起来,可见代码还是非常的厚重的,定义一个切面就要定义一个切面类,然而切面类中,就一个通知方法,着实没有必要。所以Spring提供了,依赖aspectj的schema配置和基于aspectj 注解方式。这两种方式非常简介方便使用,也是项目中普遍的使用方式。梳理之:

4、方式二:schema配置

a、业务类:

/** * 业务类 *  * @author yanbin *  */public class AspectBusiness { /** * 切入点 */ public String delete(String obj) { System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n"); return obj + ":瞄~"; } public String add(String obj) { System.out.println("================这个方法不能被切。。。============== \n"); return obj + ":瞄~ 嘿嘿!"; } public String modify(String obj) { System.out.println("=================这个也设置加入切吧====================\n"); return obj + ":瞄改瞄啊!"; } }

b、切面类:切面类中,包含了所有的通知

/** * 定义一个切面 *  * @author yanbin *  */public class AspectAdvice { /** * 前置通知 * * @param jp */ public void doBefore(JoinPoint jp) { System.out.println("===========进入before advice============ \n"); System.out.print("准备在" + jp.getTarget().getClass() + "对象上用"); System.out.print(jp.getSignature().getName() + "方法进行对 '"); System.out.print(jp.getArgs()[0] + "'进行删除!\n\n"); System.out.println("要进入切入点方法了 \n"); } /** * 后置通知 * * @param jp * 连接点 * @param result * 返回值 */ public void doAfter(JoinPoint jp, String result) { System.out.println("==========进入after advice=========== \n"); System.out.println("切入点方法执行完了 \n"); System.out.print(jp.getArgs()[0] + "在"); System.out.print(jp.getTarget().getClass() + "对象上被"); System.out.print(jp.getSignature().getName() + "方法删除了"); System.out.print("只留下:" + result + "\n\n"); } /** * 环绕通知 * * @param pjp * 连接点 */ public void doAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("===========进入around环绕方法!=========== \n"); // 调用目标方法之前执行的动作 System.out.println("调用方法之前: 执行!\n"); // 调用方法的参数 Object[] args = pjp.getArgs(); // 调用的方法名 String method = pjp.getSignature().getName(); // 获取目标对象 Object target = pjp.getTarget(); // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行 Object result = pjp.proceed(); System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + result + "\n"); System.out.println("调用方法结束:之后执行!\n"); } /** * 异常通知 * * @param jp * @param e */ public void doThrow(JoinPoint jp, Throwable e) { System.out.println("删除出错啦"); } }

c、配置文件:

d、测试类:

public class Debug {    public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("aop/schema_aop.xml"); AspectBusiness business = (AspectBusiness) context.getBean("aspectBusiness"); business.delete("猫"); } }

5、方式三:aspectj注解

注解在项目中已经到处都是了,撇开一些优劣不提,开发的便利性和可读性是非常的方便的。用来配置Spring AOP也非常简单便利

a、业务类:

/** * 业务类 *  * @author yanbin *  */ @Component public class Business { /** * 切入点 */ public String delete(String obj) { System.out.println("==========调用切入点:" + obj + "说:你敢删除我!===========\n"); return obj + ":瞄~"; } public String add(String obj) { System.out.println("================这个方法不能被切。。。============== \n"); return obj + ":瞄~ 嘿嘿!"; } public String modify(String obj) { System.out.println("=================这个也设置加入切吧====================\n"); return obj + ":瞄改瞄啊!"; } }

b、切面类:

/** * 定义切面 *  * @Aspect : 标记为切面类 * @Pointcut : 指定匹配切点 * @Before : 指定前置通知,value中指定切入点匹配 * @AfterReturning :后置通知,具有可以指定返回值 * @AfterThrowing :异常通知 *  * @author yanbin *  */ @Component @Aspect public class AspectAdvice { /** * 指定切入点匹配表达式,注意它是以方法的形式进行声明的。 */ @Pointcut("execution(* aop.annotation.*.*(..))") public void anyMethod() { } /** * 前置通知 * * @param jp */ @Before(value = "execution(* aop.annotation.*.*(..))") public void doBefore(JoinPoint jp) { System.out.println("===========进入before advice============ \n"); System.out.print("准备在" + jp.getTarget().getClass() + "对象上用"); System.out.print(jp.getSignature().getName() + "方法进行对 '"); System.out.print(jp.getArgs()[0] + "'进行删除!\n\n"); System.out.println("要进入切入点方法了 \n"); } /** * 后置通知 * * @param jp * 连接点 * @param result * 返回值 */ @AfterReturning(value = "anyMethod()", returning = "result") public void doAfter(JoinPoint jp, String result) { System.out.println("==========进入after advice=========== \n"); System.out.println("切入点方法执行完了 \n"); System.out.print(jp.getArgs()[0] + "在"); System.out.print(jp.getTarget().getClass() + "对象上被"); System.out.print(jp.getSignature().getName() + "方法删除了"); System.out.print("只留下:" + result + "\n\n"); } /** * 环绕通知 * * @param pjp * 连接点 */ @Around(value = "execution(* aop.annotation.*.*(..))") public void doAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("===========进入around环绕方法!=========== \n"); // 调用目标方法之前执行的动作 System.out.println("调用方法之前: 执行!\n"); // 调用方法的参数 Object[] args = pjp.getArgs(); // 调用的方法名 String method = pjp.getSignature().getName(); // 获取目标对象 Object target = pjp.getTarget(); // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行 Object result = pjp.proceed(); System.out.println("输出:" + args[0] + ";" + method + ";" + target + ";" + result + "\n"); System.out.println("调用方法结束:之后执行!\n"); } /** * 异常通知 * * @param jp * @param e */ @AfterThrowing(value = "execution(* aop.annotation.*.*(..))", throwing = "e") public void doThrow(JoinPoint jp, Throwable e) { System.out.println("删除出错啦"); } }

c、配置:

d、测试类:

/** * 测试类 *  * @author yanbin *  */public class Debug { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("aop/annotation_aop.xml"); Business business = (Business) context.getBean("business"); business.delete("猫"); } }

转载于:https://www.cnblogs.com/cuitrek/p/5139261.html

你可能感兴趣的文章
JDK中的Timer和TimerTask详解(zhuan)
查看>>
【python练习】ATM&购物商城程序
查看>>
nginx 日志问题(\x22)
查看>>
装饰器、迭代器、生成器
查看>>
类对象作为类成员
查看>>
面向对象和面向过程的区别及优劣对比详解
查看>>
const与指针
查看>>
thsi指针的一些用法及作用
查看>>
c++友元
查看>>
c++运算符重载
查看>>
一元运算符重载
查看>>
Windows 远程栈溢出挖掘
查看>>
(网页)the server responded with a status of 403 (Forbidden)
查看>>
葡萄城报表介绍:Java 报表
查看>>
android 通知消息一
查看>>
UNET学习笔记2 - 高级API(HLAPI)
查看>>
腾讯编程马拉松2012第一题
查看>>
Day18
查看>>
Web Service数据源
查看>>
php.ini详解(转)
查看>>