Spring4学习笔记-AOP前传之动态代理
来源:互联网 发布:sql查询分析器下载 编辑:程序博客网 时间:2024/05/10 02:59
假设有如下需求:
写一个计算器类,里面包含加减乘除四个方法。在每个方法开始前打印出该方法开始的消息,在每个方法结束前打印出该方法结束的消息和计算的结果。
普通方法,先写一个接口,然后在接口里实现四个方法。在每个方法里加上要打印的语句。实现代码如下。
ArithmeticCalculator接口
package com.spring.aop.helloworld;public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);}
ArithmeticCalculatorLoggingImpl.java 实现上面的接口
package com.spring.aop.helloworld;public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator{@Overridepublic 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 end with " + result);return result;}@Overridepublic 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 end with " + result);return result;}@Overridepublic 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 end with " + result);return result;}@Overridepublic 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 end with " + result);return result;}}
Main.java
ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorLoggingImpl();arithmeticCalculator.add(1, 5);System.out.println("----------");arithmeticCalculator.sub(5, 3);System.out.println("----------");arithmeticCalculator.mul(3, 7);System.out.println("----------");arithmeticCalculator.div(9, 3);
程序运行结果:
The method add begins with [1, 5]
The method add end with 6
----------
The method sub begins with [5, 3]
The method sub end with 2
----------
The method mul begins with [3, 7]
The method mul end with 21
----------
The method div begins with [9, 3]
The method div end with 3
可见,上面的代码中间存在这大量相似的代码。而面向对象编程又不能很好地解决这个问题,下面采用动态代理的方法来解决上面的问题。
接口不变。写一个实现类ArithmeticCalculatorImpl.java 这个实现类只关注业务,没有需要打印的内容
package com.spring.aop.helloworld;public class ArithmeticCalculatorImpl implements ArithmeticCalculator{@Overridepublic int add(int i, int j) {int result = i + j;return result;}@Overridepublic int sub(int i, int j) {int result = i - j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}@Overridepublic int div(int i, int j) {int result = i / j;return result;}}
ArithmeticCaculatorLogginProxy.java
package com.spring.aop.helloworld;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.Arrays;public class ArithmeticCaculatorLogginProxy {//要代理的对象private ArithmeticCalculator target;public ArithmeticCaculatorLogginProxy(ArithmeticCalculator target){this.target = target;}public ArithmeticCalculator getLoggingProxy() {ArithmeticCalculator proxy = null;//代理对象由哪一个类加载器负责加载ClassLoader loader = target.getClass().getClassLoader();//代理对象的类型,即其中有哪些方法Class[] interfaces = new Class[]{ArithmeticCalculator.class};//当调用代理对象其中的方法时,该执行的代码InvocationHandler h = new InvocationHandler() {/** * proxy:正在返回的代理对象,一般情况下,在invoke方法中都不适用该对象 * method:正在被调用的方法 * args:调用方法时,传入的参数 */@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {//下面这句执行的时候又会调用invoke方法,所以会出现死循环导致内存溢出//System.out.println(proxy.toString());String methodName = method.getName();//日志System.out.println("The method " + methodName + " begins with" + Arrays.asList(args));System.out.println("Invoke...");//执行方法Object result = method.invoke(target, args);//日志System.out.println("The method" + methodName + " ends with " + result);return result;}};proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);return proxy;}}
main方法
ArithmeticCalculator target = new ArithmeticCalculatorImpl();ArithmeticCalculator proxy = new ArithmeticCaculatorLogginProxy(target).getLoggingProxy();proxy.add(1, 5);System.out.println("----------");proxy.sub(5, 3);System.out.println("----------");proxy.mul(3, 7);System.out.println("----------");proxy.div(9, 3);
程序运行结果 :
The method add begins with[1, 5]
Invoke...
The methodadd ends with 6
----------
The method sub begins with[5, 3]
Invoke...
The methodsub ends with 2
----------
The method mul begins with[3, 7]
Invoke...
The methodmul ends with 21
----------
The method div begins with[9, 3]
Invoke...
The methoddiv ends with 3
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1557431
- Spring4学习笔记-AOP前传之动态代理
- Spring笔记(三)AOP前篇之动态代理
- Spring4.0学习笔记007——AOP基础:动态代理、概念解析
- Spring4笔记----动态代理
- Spring4学习笔记-AOP
- Spring学习笔记之aop动态代理(3)
- Spring4.0学习笔记--AOP
- Spring4.0学习笔记-AOP
- Spring AOP 学习之cJlib动态代理
- AOP之动态代理
- aop之动态代理
- web学习笔记19-静态代理,动态代理,spring AOP
- AOP代理之动态代理
- AOP代理之动态代理
- java学习之路----静态代理---动态代理-----AOP的前奏(AOP也是动态代理)
- J2EE系列之Spring4学习笔记(七)--AOP详解(AOP简介)
- J2EE系列之Spring4学习笔记(八)--AOP详解(AOP实例)
- spring学习笔记(12)--Introduction-AOP中的动态代理
- javascript使用正则表达式
- 重金属项目笔记
- servlet复习(五)cookie管理
- Spring4学习笔记
- Spring4学习笔记-泛型依赖注入
- Spring4学习笔记-AOP前传之动态代理
- 【Leetcode】之Permutations II
- 招聘相关笔记
- Spring4学习笔记-AOP(基于注解的方式)
- Spring4学习笔记-AOP(基于配置文件的方式)
- Spring4学习笔记-JDBC
- Spring4学习笔记-声明式事务(基于注解的方式)
- Spring4学习笔记-声明式事务(基于配置文件的方式)
- Spring4学习笔记-Spring4整合Struts2(基于配置文件的方式)