一. 回顾
前面Day22——AOP前奏讲到了计算器的日志功能问题,由于代码散乱,需要将日志功能独立出来实现,需要用到动态代理。今天讲述动态代理。
本文章项目源码已上传到本博客的“资源”,可自行前往免费下载
二. 原理
2.1 动态代理的原理
代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理对象“取代”原始对象,任何原始对象的调用都需要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。
动态代理原理: 在运行时,动态地给原始对象(即目标对象)生成一个代理对象,代理对象可以增强原始对象的功能(即扩展功能),代理对象只能完成扩展出来的功能,而原始对象的功能还必须由原始对象去完成。但是,代理对象决定是否以及何时将功能转回到原始对象上。
2.2 JDK动态代理
- Proxy:所有动态代理的父类。主要用于生成代理类,代理对象。
//返回代理类的Class对象
public static Class<?> getProxyClass(Classloader loader, Class<?>...interface);
//返回代理对象
public static Object newProxyInstance(Classloader loader, Class<?>[]...interface, InvocationHandler h);
- InvocationHandler:完成代理对象功能的扩展。代理对象要做什么事,通过InvocationHandler中的invoke方法来完成
public Object invoke(Object proxy, Method method, Object[] args);
二. 例子
2.1 使用newProxyInstance方式获取代理对象
ArithmeticCalculator.java
package com.atguigu.spring.aop.before;
/**
* 算数计算器
* @author user
*
*/
public interface ArithmeticCalculator {
/**
* 加法
* @param i
* @param j
* @return
*/
public int add(int i, int j);
/**
* 减法
* @param i
* @param j
* @return
*/
public int sub(int i, int j);
/**
* 乘法
* @param i
* @param j
* @return
*/
public int mul(int i, int j);
/**
* 除法
* @param i
* @param j
* @return
*/
public int div(int i, int j);
}
ArithmeticCalculatorImpl.java
package com.atguigu.spring.aop.before;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
// TODO Auto-generated method stub
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
// TODO Auto-generated method stub
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
// TODO Auto-generated method stub
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
// TODO Auto-generated method stub
int result = i / j;
return result;
}
}
ArithmeticCalculatorProxy.java
package com.atguigu.spring.aop.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import com.atguigu.spring.aop.before.ArithmeticCalculator;
public class ArithmeticCalculatorProxy {
private ArithmeticCalculator target;
//想通过构造器从外面传进来目标对象,并赋值给本类中的target
public ArithmeticCalculatorProxy(ArithmeticCalculator target) {
this.target = target;
}
/**
* 获取代理对象
*/
public Object getProxy() {
Object proxy;//代理对象
//获取代理对象
//1. loader: Classloader类加载器
ClassLoader loader = target.getClass().getClassLoader();
//2. interface: 目标类实现的所有接口。目的是代理类也要实现这些接口,保证代理类 与 目标类中的方法是一致的
Class[] interfaces = target.getClass().getInterfaces();
//3. h: InvocationHandler 完成动态代理的整个过程
proxy = Proxy.newProxyInstance(loader, interfaces, new InvocationHandler() {
/**
* Object proxy: 代理对象
* Method method: 正在被调用的方法
* Object[] args: 方法的参数列表
*
* 首先执行的是newProxyInstance,所以会执行new InvocationHandler(),
* 但是并不会执行invoke方法,执行完newProxyInstance后,
* 就new到一个proxy了
* 当invoke被调用才会执行invoke方法
*
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
String methodName = method.getName();
//日志记录
System.out.println("Proxy==> The method " + methodName + " begins with : " + Arrays.asList(args));
//代理对象将功能转回到原本对象
Object result = method.invoke(target, args);
//日志记录
System.out.println("Proxy==> The method " + methodName + " ends with : " + result);
return result;
}
});
return proxy;
}
}
Main.java
package com.atguigu.spring.aop.before;
import com.atguigu.spring.aop.proxy.ArithmeticCalculatorProxy;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//目标对象
ArithmeticCalculator target = new ArithmeticCalculatorImpl();
//代理对象
Object obj = new ArithmeticCalculatorProxy(target).getProxy();
//强制转换成接口类型
ArithmeticCalculator proxy = (ArithmeticCalculator)obj;
//代理对象调用方法
int result = proxy.add(1, 1);
System.out.println("Main==>:" + result);
}
}
2.2 用getProxyClass方法获取动态代理对象
只需修改ArithmeticCalculatorProxy.java中的getProxy()方法即可。
/**
* 用getProxyClass方式获取代理对象
*/
public Object getProxy2() throws Exception {
Object proxy;//代理对象
ClassLoader loader = target.getClass().getClassLoader();
Class[] interfaces = target.getClass().getInterfaces();
//先获取代理类
Class proxyClass = Proxy.getProxyClass(loader, interfaces);
//在获取代理对象
//先获取代理类中的带参数的构造器
Constructor con =
proxyClass.getDeclaredConstructor(InvocationHandler.class);
proxy = con.newInstance(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
String methodName = method.getName();
//日志记录
System.out.println("Proxy==> The method " + methodName + " begins with : " + Arrays.asList(args));
//代理对象将功能转回到原本对象
Object result = method.invoke(target, args);
//日志记录
System.out.println("Proxy==> The method " + methodName + " ends with : " + result);
return result;
}
});
return proxy;
}