Spring AOP 5种通知与java动态代理
- @Component("arithmeticCalculator")
- public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
- @Override
- public int add(int i, int j) {
- int result = i + j;
- return result;
- }
- @Override
- public int sub(int i, int j) {
- int result = i - j;
- return result;
- }
- @Override
- public int mul(int i, int j) {
- int result = i * j;
- return result;
- }
- @Override
- public int div(int i, int j) {
- int result = i / j;
- return result;
- }
- }
接口的实现类
- public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {
- @Override
- public int add(int i, int j) {
- System.out.println("The method add begins with [" + i + "," + j + "]");
- int result = i + j;
- System.out.println("The method add ends with " + result);
- return result;
- }
- @Override
- public int sub(int i, int j) {
- System.out.println("The method sub begins with [" + i + "," + j + "]");
- int result = i - j;
- System.out.println("The method sub ends with " + result);
- return result;
- }
- @Override
- public int mul(int i, int j) {
- System.out.println("The method mul begins with [" + i + "," + j + "]");
- int result = i * j;
- System.out.println("The method mul ends with " + result);
- return result;
- }
- @Override
- public int div(int i, int j) {
- System.out.println("The method div begins with [" + i + "," + j + "]");
- int result = i / j;
- System.out.println("The method div ends with " + result);
- return result;
- }
- }
方法一:手动实现为方法添加日志
- public class ArithmeticCalculatorLoggingProxy {
- private ArithmeticCalculator target;
- public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
- super();
- this.target = target;
- }
- public ArithmeticCalculator getLoggingProxy(){
- ArithmeticCalculator proxy = null;
- ClassLoader loader = target.getClass().getClassLoader();
- Class [] interfaces = new Class[]{ArithmeticCalculator.class};
- InvocationHandler h = new InvocationHandler() {
- /*method 方法
- *args 参数
- * */
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- String methodName = method.getName();
- //前置通知
- System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));
- Object result = null;
- try {
- result = method.invoke(target, args);//返回通知
- } catch (NullPointerException e) {
- e.printStackTrace();
- //异常通知
- }
- //后置通知
- System.out.println("[after] The method ends with " + result);
- return result;
- }
- };
- proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
- return proxy;
- }
- }
方法二:利用动态代理模式实现为方法添加日志
- /**
- * AOP
- * 1. 导入jar包
- * com.springsource.net.sf.cglib-2.2.0.jar
- * com.springsource.org.aopalliance-1.0.0.jar
- * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
- * spring-aspects-4.0.0.RELEASE.jar
- *
- * 2. 在配置文件中加入AOP的命名空间
- * xmlns:aop="http://www.springframework.org/schema/aop"
- *
- * 3. 基于注解的方式
- * <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
- * <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- *
- * 4.把横切关注点的代码抽象到切面的类中
- * @Aspect
- * @Component
- * public class LoggingAspect {}
- *
- * 5.AspectJ支持5种类型的通知注解
- * @Before()前置通知,在方法开始前执行
- * @After()后置通知,在方法执行后执行,无论抛异常都会执行;不能访问方法执行的结果
- * @AfterRunning()返回通知,在方法返回结果后执行(即方法正常结束后才执行);可以得到方法执行的结果
- * @AfterThrowing()异常通知,在方法抛出异常时执行
- * @Around()环绕通知,围绕着方法执行
- * 6.AspectJ表达式
- * execution(public int com.aop.beans.ArithmeticCalculator.*(int, int))
- * execution(* com.aop.beans.*.*(int, int))
- *
- * 7.JoinPoint
- * 通过他可以得到方法的信息
- */
- //把这个类放入到IOC容器中@Component;再声明为一个切面@Aspect
- @Aspect
- @Component
- public class LoggingAspect {
- //该方法是一个前置通知,即在目标方法开始前执行@Before()
- @Before("execution(public int com.aop.beans.ArithmeticCalculator.*(int, int))")
- public void beforeMethod(JoinPoint joinPoint){
- String methodName = joinPoint.getSignature().getName();//得到方法名
- Object [] args = joinPoint.getArgs();//得到参数
- System.out.println("前置通知:The method " + methodName + " begins with " + Arrays.asList(args));
- }
- @After("execution(* com.aop.beans.*.*(..))")//位置为这个包下的所有类、所有方法、不论参数是什么类型
- public void afterMethod(JoinPoint joinPoint){
- String methodName = joinPoint.getSignature().getName();
- System.out.println("后置通知:The method " + methodName + " ends");
- }
- @AfterReturning(value="execution(public int com.aop.beans.ArithmeticCalculator.*(int, int))",
- returning="result")
- public void afterReturning(JoinPoint joinPoint, Object result){
- String methodName = joinPoint.getSignature().getName();
- System.out.println("返回通知:The method " + methodName + " ends with " + result);
- }
- @AfterThrowing(value="execution(public int com.aop.beans.ArithmeticCalculator.*(int, int))",
- throwing="e")
- public void afterThrowing(JoinPoint joinPoint, Exception e){
- String methodName = joinPoint.getSignature().getName();
- System.out.println("异常通知:The method " + methodName + " occurs excetion:" + e);
- }
- /*
- * 环绕通知功能相当于动态代理的全过程,需要有ProceedingJoinPoint 类型的参数;
- * pjd 参数可以决定是否执行目标方法
- * 环绕通知必须有返回值,返回值即为目标方法的返回值
- *
- *
- @Around("execution(public int com.aop.beans.ArithmeticCalculator.*(int, int))")
- public Object aroundMethod(ProceedingJoinPoint pjd){
- Object result = null;//返回值
- String methodName = pjd.getSignature().getName();//得到方法名
- try {
- //前置通知
- System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
- result = pjd.proceed();//执行方法 //返回通知
- System.out.println("The method " + methodName + " ends with " + result);
- } catch (Throwable e) {
- //异常通知
- System.out.println("The method " + methodName + " occurs exception:" + e);
- throw new RuntimeException(e);
- }
- //后置通知
- System.out.println("The method " + methodName + " ends");
- return result;
- }
- */
- }
方法三:利用Spring AOP AspectJ实现为方法添加日志
- <?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"
- xmlns:context="http://www.springframework.org/schema/context"
- 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.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- 自动扫描的包; 扫描@注解,添加到IOC容器中,让Spring管理-->
- <context:component-scan base-package="com.aop.beans"></context:component-scan>
- <!-- 使 AspectJ 的注解起作用 ; autoproxy自动代理-->
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- </beans>
beans-aop.xml
- public class Main {
- public static void main(String[] args) {
- /*代理模式实现添加日志功能
- ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl();
- arithmeticCalculator = new ArithmeticCalculatorLoggingProxy(arithmeticCalculator).getLoggingProxy();
- int result = arithmeticCalculator.add(11, 12);
- System.out.println("result:" + result);
- result = arithmeticCalculator.div(21, 3);
- System.out.println("result:" + result);
- */
- //AOP实现添加日志功能
- ApplicationContext ctx = new ClassPathXmlApplicationContext("com/aop/beans/beans-aop.xml");
- ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
- System.out.println(arithmeticCalculator.getClass().getName());
- int result = arithmeticCalculator.add(11, 12);
- System.out.println("result:" + result);
- result = arithmeticCalculator.div(21, 2);
- System.out.println("result:" + result);
- }
- }
测试类
Spring AOP 5种通知与java动态代理的更多相关文章
- Spring AOP中的JDK和CGLib动态代理哪个效率更高?
一.背景 今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib动态代理哪个效率更高? 二.基本概念 首先,我们知道Spring AOP的底层实现有两种方式:一种是JDK动态代理, ...
- Spring AOP中的JDK和CGLIB动态代理
Spring在将Advice织入目标对象的Joinpoint是在运行时动态进行的.它采用的方式可能有两种,即JDK动态代理与CGLIB代理.Spring会根据具体的情况在两者之间切换. 实际情况如下: ...
- Spring AOP简介与底层实现机制——动态代理
AOP简介 AOP (Aspect Oriented Programing) 称为:面向切面编程,它是一种编程思想.AOP 是 OOP(面向对象编程 Object Oriented Programmi ...
- java面试题之spring aop中jdk和cglib哪个动态代理的性能更好?
在jdk6和jdk7的时候,jdk比cglib要慢: 在jdk8的时候,jdk性能得到提升比cglib要快很多: 结论出自:https://www.cnblogs.com/xuliugen/p/104 ...
- JAVA动态代理的全面深层理解
Java 动态代理机制的出现,使得 Java 开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象,便能动态地获得代理类.代理类会负责将所有的方法调用分派到委托对象上反射执行,在分派执行的过 ...
- 一文读懂Java动态代理
作者 :潘潘 日期 :2020-11-22 事实上,对于很多Java编程人员来说,可能只需要达到从入门到上手的编程水准,就能很好的完成大部分研发工作.除非自己强主动获取,或者工作倒逼你学习,否则我们好 ...
- 深入浅出Java动态代理
文章首发于[博客园-陈树义],点击跳转到原文深入浅出Java动态代理 代理模式是设计模式中非常重要的一种类型,而设计模式又是编程中非常重要的知识点,特别是在业务系统的重构中,更是有举足轻重的地位.代理 ...
- Java动态代理 深度详解
代理模式是设计模式中非常重要的一种类型,而设计模式又是编程中非常重要的知识点,特别是在业务系统的重构中,更是有举足轻重的地位.代理模式从类型上来说,可以分为静态代理和动态代理两种类型. 今天我将用非常 ...
- Java动态代理:一个面包店的动态代理帝国
文章首发于[博客园-陈树义],点击跳转到原文大白话说Java动态代理:一个面包店的动态代理帝国 代理模式是设计模式中非常重要的一种类型,而设计模式又是编程中非常重要的知识点,特别是在业务系统的重构中, ...
随机推荐
- github添加ssh key报错Key is invalid. Ensure you've copied the file correctly
github添加ssh key的时候报错:Key is invalid. Ensure you've copied the file correctly 将秘钥复制粘贴到文本编辑器中,再粘贴复制到
- VS2012 asp.net mvc 4 运行项目提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败"
创建mvc4 应用程序发布,运行出错.出现未经授权: 服务器配置导致登录失败.请验证您是否有权基于您提供的凭,后来找得解决方法: 打开点站的web.confg文件,将: <authorizati ...
- jQuery原型属性和方法总结
从大四下学期开始了解jquery源码相关的东西,在回校参加毕业典礼(准确的说是参加补考挂科太多)期间便开始借着<jQuery>内幕学习jquery源码,然后在博客园写笔记也已经两个月了,也 ...
- 优秀的CSS框架---bootstrap
Bootstrap是Twitter推出的一个用于前端开发的开源工具包.它 由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架.现在在网上已经有 ...
- ST05 跟踪SQL
SAP R/3 提供标准ABAP SQL 跟踪工具.使用T-Code:ST05 可以进入追踪设定画面: 在Trace Modes 区域中选择需要在SAP R/3 Serv ...
- 如何:对 SharePoint 列表项隐藏 ECB 中的菜单项
可以通过使用功能框架向编辑控制块 (ECB) 菜单添加新的自定义操作.但是,您不能使用此方法进行相反的操作,即隐藏现有的 ECB 菜单项,因为它们是通过使用 ECMAScript(JavaScript ...
- [Dynamics CRM 2016]如何配置多语言显示
1.安装相对应的语言包并安装 2015语言包下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=45014 2016语言包下载地 ...
- Android Tips: 打电话和发短信
利用Android打电话非常简单,直接调用Android内在的电话功能就可以了. btnDail.setOnClickListener(new OnClickListener(){ @Override ...
- 属性(@property)、@synthesize
先前我们学的实例变量是这样的 { int _age; int _height; int age; } 后来学属性 @property int age; 看到@property 会自动编译生成某个成员变 ...
- Kotlin语法(函数和lambda表达式)
三.函数和lambda表达式 1. 函数声明 fun double(x: Int): Int { } 函数参数是用 Pascal 符号定义的 name:type.参数之间用逗号隔开,每个参数必须指明类 ...