标签:spring img opened gif 功能 关联 src throws pre
假设需实现一个计算的类Math、完成加、减、乘、除功能,如下所示:
Math类:
public class Math { //加 public int add(int n1,int n2){ int result=n1+n2; System.out.println(n1+"+"+n2+"="+result); return result; } //减 public int sub(int n1,int n2){ int result=n1-n2; System.out.println(n1+"-"+n2+"="+result); return result; } //乘 public int mut(int n1,int n2){ int result=n1*n2; System.out.println(n1+"X"+n2+"="+result); return result; } //除 public int div(int n1,int n2){ int result=n1/n2; System.out.println(n1+"/"+n2+"="+result); return result; } }
现在需求发生了变化,要求项目中所有的类在执行方法时输出执行耗时。最直接的办法是修改源代码,如下所示:
public class Math { //加 public int add(int n1,int n2){ //开始时间 long start = getTime(); delay(); int result=n1+n2; System.out.println(n1+"+"+n2+"="+result); System.out.println("共用时:" + (getTime() - start)); return result; } //减 public int sub(int n1,int n2){ //开始时间 long start = getTime(); delay(); int result=n1*n2; System.out.println(n1+"-"+n2+"="+result); System.out.println("共用时:" + (getTime() - start)); return result; } //乘 public int mut(int n1,int n2){ //开始时间 long start = getTime(); delay(); int result=n1*n2; System.out.println(n1+"X"+n2+"="+result); System.out.println("共用时:" + (getTime() - start)); return result; } //除 public int div(int n1,int n2){ //开始时间 long start = getTime(); delay(); int result=n1/n2; System.out.println(n1+"/"+n2+"="+result); System.out.println("共用时:" + (getTime() - start)); return result; } public static long getTime() { return System.currentTimeMillis(); } public static void delay(){ try { int n = (int) new Random().nextInt(500); Thread.sleep(n); } catch (InterruptedException e) { e.printStackTrace(); } } }
测试运行:
Math math = new Math(); int n=100; int m=5; math.add(n,m); math.sub(n,m); math.mut(n,m); math.div(n,m);
1、定义抽象主题接口。
IMath:
public interface IMath { int add(int n1,int n2); int sub(int n1,int n2); int mut(int n1,int n2); int div(int n1,int n2); }
2、实现接口:
MathImpl
public class MathImpl implements IMath{ //加 public int add(int n1,int n2){ int result=n1+n2; System.out.println(n1+"+"+n2+"="+result); return result; } //减 public int sub(int n1,int n2){ int result=n1-n2; System.out.println(n1+"-"+n2+"="+result); return result; } //乘 public int mut(int n1,int n2){ int result=n1*n2; System.out.println(n1+"X"+n2+"="+result); return result; } //除 public int div(int n1,int n2){ int result=n1/n2; System.out.println(n1+"/"+n2+"="+result); return result; } }
3、代理类
MathProxy
public class MathProxy implements IMath { IMath math = new MathImpl(); @Override public int add(int n1, int n2) { //开始时间 long start = getTime(); delay(); int result = math.add(n1, n2); System.out.println("共用时:" + (getTime() - start)); return result; } @Override public int sub(int n1, int n2) { //开始时间 long start = getTime(); delay(); int result = math.sub(n1, n2); System.out.println("共用时:" + (getTime() - start)); return result; } @Override public int mut(int n1, int n2) { //开始时间 long start = getTime(); delay(); int result = math.mut(n1, n2); System.out.println("共用时:" + (getTime() - start)); return result; } @Override public int div(int n1, int n2) { //开始时间 long start = getTime(); delay(); int result = math.div(n1, n2); System.out.println("共用时:" + (getTime() - start)); return result; } public static long getTime() { return System.currentTimeMillis(); } public static void delay(){ try { int n = (int) new Random().nextInt(500); Thread.sleep(n); } catch (InterruptedException e) { e.printStackTrace(); } } }
测试运行:
IMath math = new MathProxy(); int n1=100,n2=5; math.add(n1, n2); math.sub(n1, n2); math.mut(n1, n2); math.div(n1, n2);
DynamicProxy
package com.wbg.springAOP.springdynamic; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Random; /** * 动态代理类 */ public class DynamicProxy implements InvocationHandler { IMath math = new MathImpl(); //目标代理对象 Object targetObject; public DynamicProxy(Object target){ this.targetObject=target; } public DynamicProxy(){ } /** * 获得被代理后的对象 * @param object 被代理的对象 * @return 代理后的对象 */ public Object getProxyObject(Object object){ this.targetObject=object; return Proxy.newProxyInstance( targetObject.getClass().getClassLoader(), //类加载器 targetObject.getClass().getInterfaces(), //获得被代理对象的所有接口 this); //InvocationHandler对象 //loader:一个ClassLoader对象,定义了由哪个ClassLoader对象来生成代理对象进行加载 //interfaces:一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了 //h:一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上,间接通过invoke来执行 } /** * 获取时间 * @return */ public static long getTime() { return System.currentTimeMillis(); } /** * 延迟 */ public static void delay(){ try { int n = (int) new Random().nextInt(500); Thread.sleep(n); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 当用户调用对象中的每个方法时都通过下面的方法执行,方法必须在接口 * proxy 被代理后的对象 * method 将要被执行的方法信息(反射) * args 执行方法时需要的参数 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { long start=getTime(); delay(); Object result=method.invoke(targetObject,args); System.out.println("共用时:" + (getTime() - start)); return result; } }
DynamicProxy dynamicProx = new DynamicProxy(new MathImpl()); IMath math = (IMath) Proxy.newProxyInstance(Test.class.getClassLoader(),new Class[]{IMath.class},dynamicProx); int n1=100,n2=5; math.add(n1, n2); math.sub(n1, n2); math.mut(n1, n2); math.div(n1, n2);
//实例化一个MathProxy代理对象 //通过getProxyObject方法获得被代理后的对象 IMath math=(IMath)new DynamicProxy().getProxyObject(new MathImpl()); int n1=100,n2=5; math.add(n1, n2); math.sub(n1, n2); math.mut(n1, n2); math.div(n1, n2);
标签:spring img opened gif 功能 关联 src throws pre
原文地址:https://www.cnblogs.com/weibanggang/p/10133965.html