十.SpringAOP的Advice(通知)类型及对编码实现的理解
2019-08-21 09:25
1691 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41857955/article/details/99935628
SpringAOP的四种类型
- 前置通知类型:在目标方法前实施增强
- 后置通知类型:在目标方法后实施增强
- 环绕通知类型:在目标方法执行前后实施增强
- 异常通知类型:在方法出现异常之后抛出
- 引介通知类型:在目标方法中添加一些属性和方法
通过代码实现
通过一个简单的用户浏览商品的例子去理解前四种通知类型
1.定义一个接口ProductInfoService
package com.tb.service; public interface ProductInfoService { //用户名和商品名 public void browse(String name,String productname); }
2.定义接口的实现类ProductInfoServiceImpl
package com.tb.service; public class ProductInfoServiceImpl implements ProductInfoService { @Override public void browse(String name, String productname) { //int a=5/0;测试异常通知 System.out.println("as"); //用一个循环方便观察测试 int i=20; while(i>0){ System.out.println(i--); } } }
3.定义测试四种异常的类AllLogAdvice
package com.tb.aop; import java.text.SimpleDateFormat; import java.util.Date; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class AllLogAdvice { public void mybeforeAdvice(JoinPoint joinpoint) { // 前置通知,获得参数 Object[] args =joinpoint.getArgs(); Date d = new Date(); String browser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d); String logInfotext = args[0] + browser + "浏览" + args[1]; System.out.println(logInfotext); } public void myAfterAdvice(JoinPoint joinpoint) { //后置通知,获取参数 Object[] args = joinpoint.getArgs(); Date d = new Date(); String browser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d); String logInfotext = args[0] + browser + "浏览" + args[1]; System.out.println(logInfotext); } public void myexceptionAdvice(JoinPoint joinpoint) { //异常通知 String classname = joinpoint.getClass().getName(); String methodname = joinpoint.getSignature().getName(); Date d = new Date(); String exceptdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d); String logInfotext = exceptdate + classname + methodname + "异常"; System.out.println(logInfotext); } public void myArodAdvice(ProceedingJoinPoint pjoinPoint) throws Throwable { //环绕通知 System.out.println("环绕通知开始执行!"); long starttime = System.currentTimeMillis(); pjoinPoint.proceed(); long endtime = System.currentTimeMillis(); String methodname = pjoinPoint.getSignature().getName(); String logInfotext = methodname + "time is" + (endtime - starttime); System.out.println(logInfotext); } }
4.配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //添加AOP的命名空间 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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-4.2.xsd"> <bean id="productInfoService" class="com.tb.service.ProductInfoServiceImpl"> </bean> <bean id="allLogAdvice" class="com.tb.aop.AllLogAdvice"></bean> <!-- AOP切面编程: <aop:aspect>只有一个通知和一个切入点 <aop:pointcut>切入点从具体的目标对象获取方法 --> <aop:config> <aop:aspect id="logaop" ref="allLogAdvice"> <aop:pointcut expression="execution(* com.tb.service.ProductInfoService.*(..))" id="logpoint"/> <!--前置通知切入 --> <aop:before method="mybeforeAdvice" pointcut-ref="logpoint"/> <!--后置通知切入 --> <aop:after-returning method="myAfterAdvice" pointcut-ref="logpoint"/> <!--异常通知知切入 --> <aop:after-throwing method="myexceptionAdvice" pointcut-ref="logpoint" throwing="e"/> <!--环绕通知切入 --> <aop:around method="myArodAdvice" pointcut-ref="logpoint"/> </aop:aspect> </aop:config> </beans>
5.定义测试类TestAop
package com.tb.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tb.service.ProductInfoService; public class TestAop { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); ProductInfoService ps=(ProductInfoService)ac.getBean("productInfoService"); ps.browse("張三", "華爲p10"); } }
6.运行结果
相关文章推荐
- 对面试题“输入n,求一个nXn矩阵,规定矩阵沿45度递增,形成一个zigzag数组(JPEG编码里取像素数据的排列顺序),请问如何用C++实现?”的理解
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 正确理解RESET类型的INPUT控件实现重置效果的机制
- [java]深入理解java中的char,unicode编码及其实现方式
- Redis学习笔记(十五)Redis数据类型底层编码和实现原理
- 请编码实现:实现一个对8Bit数据(unsigned char 类型)的指定位(例如第n位)的置0或者置一操作,并保持其他位不变。
- Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate和JsonTime自定义类型如何实现json编码和解码(二)
- 基于代理类ProxyFactoryBean的AOP实现---前置通知编码实现
- 【框架】[Spring]纯Java方式实现AOP拦截-详解ThrowsAdvice异常通知
- PHP mb_convert_encoding 获取字符串编码类型实现代码
- SpringAop的理解以及实现原理
- Lucene是如何理解文档的 & 文档类型(Types)是如何被实现的
- 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
- 深入理解PHP内核(十)变量及数据类型-类型提示的实现
- ANSI Unicod UTF-8 编码 及其C++读取三种类型文档实现
- PHP mb_convert_encoding 获取字符串编码类型实现代码
- 关于对Spring中AOP的Advice(通知、增强)的属性Around的理解
- iOS-键值编码和键值监听的理解和实现
- [李景山php] 深入理解PHP内核[读书笔记]--第三章:变量及数据类型--变量的结构和类型--类型提示的实现
- 编码类型及python中实现转换