Table of contents
1. Introduction
The agency model is actually aboutWithout changing the target object methodperform on methodEnhance
This article will start fromBased on interfaceas well asBased on subclassway to explain dynamic proxy
2. Interface-based dynamic proxy
core:
The newProxyInstance method in the Proxy class officially provided by JDK
case analysis:
this is the needMethod enhancementof producers
public class Producer implements IProducer {
public void saleProduct(Float money){
System.out.println("销售产品,并拿到钱:"+ money);
}
public void afterService(Float money){
System.out.println("提供售后,并拿到钱:"+money);
}
}
Use newProxyInstance() in the Proxy class to enhance the method
* 作用:执行被代理对象的任何接口方法都会经过该方法
* 方法参数的含义
* @param proxy 代理对象的引用
* @param method 当前执行的方法
* @param args 当前执行方法所需的参数
* @return 和被代理对象方法有相同的返回值
* @throws Throwable
*/
public static void main(String[] args) {
final IProducer producer = new Producer();
IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),
producer.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//提供增强的代码
Object returnValue = null;
//1.获取方法执行的参数
Float money = (Float)args[0];
//2.判断当前方法是不是销售
if("saleProduct".equals(method.getName())) {
returnValue = method.invoke(producer, money*0.8f);
}
return returnValue;
}
});
}
Understanding of the case:
Because we need to enhance the methods in the interface, we use newProxyInstance toCreate proxy object
Calling methods in the interface willAfter the invoke method
The invoke method isResponsible for method enhancements
3. Dynamic proxy based on subclasses
core:
The create method in the Enhancer class provided by cglib
Note: The dependencies of cglib need to be imported
case analysis:
Like the previous case, this also requiresenhanced method
public class Producer {
public void saleProduct(Float money){
System.out.println("销售产品,并拿到钱:"+ money);
}
public void afterService(Float money){
System.out.println("提供售后,并拿到钱:"+money);
}
}
Use create() in cglib to enhance the method
* 执行被代理对象的任何方法都会经过该方法
* @param proxy
* @param method
* @param args
* 以上三个参数和基于接口的动态代理中invoke方法的参数是一样的
* @param methodProxy :当前执行方法的代理对象
* @return
* @throws Throwable
public class Client {
public static void main(String[] args) {
final Producer producer = new Producer();
Producer cglibPro = (Producer) Enhancer.create(Producer.class, new MethodInterceptor() {
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
//提供增强的代码
Object returnValue = null;
//1.获取方法执行的参数
Float money = (Float)args[0];
//2.判断当前方法是不是销售
if("saleProduct".equals(method.getName())) {
returnValue = method.invoke(producer, money*0.8f);
}
return returnValue;
}
});
}
Understanding of the case:
Use Enhancer.create toCreate proxy object
Calling methods in the interface willAfter intercept method
The intercept method isResponsible for method enhancements
4. Summary:
The code logic of the two methods of dynamic proxy is similar.
- Create proxy object
- Get the parameters of the current method
- Use the method name to determine whether it is a specific method
- Execute method.invoke method
- Use the proxy object you created