Dynamic Agent:
Features: use with the bytecode to create, with the use loading
effect: not modify the source code on the basis of the method of enhancing
Classification:
Based on Dynamic agent interface
dynamic proxy subclasses based
First, based on dynamic agent interface:
Class involved: Proxy
Provider: the JDK
how to create a proxy object:
using newProxyInstance methods of the Proxy class
creation requires proxy objects:
the proxy class happened implements an interface, if not can not be used
newProxyInstance method parameters:
ClassLoader: class loader
proxy object for loading bytecode, and the same proxy object class loader
class []: bytecode array
to allow the object and a proxy agent objects with the same method
InvocationHandler: code for providing enhanced
let how to write agents are generally the class that implements the interface, are usually anonymous inner classes, but not necessary
public interface IProducer {
public void saleProduct(Float money);
public void afterService(Float money);
}
public class Producer implements IProducer{
/**
* 销售
* @param money
*/
public void saleProduct(Float money){
System.out.println("销售产品,并拿到钱:"+money);
}
/**
* 售后
* @param money
*/
public void afterService(Float money){
System.out.println("提供售后服务,并拿到钱:"+money);
}
}
import com.ycl.proxy.IProducer;
import com.ycl.proxy.Producer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 模拟一个消费者
*/
public class Client {
public static void main(String[] args) {
final Producer producer = new Producer();
IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),
producer.getClass().getInterfaces(), new InvocationHandler() {
/**
* 作用:执行被代理对象的任何接口方法都会经过该方法
* @param proxy 代理对象的引用
* @param method 当前执行的方法
* @param args 当前执行方法所需的参数
* @return 和被代理对象有相同的返回值
* @throws Throwable
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//增强的代码
Object value = null;
Float money = (Float) args[0];
if ("saleProduct".equals(method.getName())){
value = method.invoke(producer,money*0.8f);
}
return value;
}
});
proxyProducer.saleProduct(10000f);
}
}
Second, based on dynamic proxy subclasses:
Related categories: Enhancer
Provider: third-party libraries cglib
how to create a proxy object:
create method Enhancer class
to create a proxy object request:
the proxy class can not be the final class
parameter create method:
Class: byte code
specifies the proxy object bytecode
Callback: code for providing enhanced
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* 模拟一个消费者
*/
public class Client {
public static void main(String[] args) {
final Producer producer = new Producer();
Producer cglibEnhancer = (Producer) Enhancer.create(producer.getClass(), new MethodInterceptor() {
/**
* 作用:执行被代理对象的任何接口方法都会经过该方法
* @param o
* @param method
* @param objects
* 以上三个参数和基于接口的动态代理中invoke方法的参数是一样的
* @param methodProxy:当前执行方法的代理对象
* @return
* @throws Throwable
*/
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
//增强的代码
Object value = null;
Float money = (Float) objects[0];
if ("saleProduct".equals(method.getName())){
value = method.invoke(producer,money*0.8f);
}
return value;
}
});
cglibEnhancer.saleProduct(10000f);
}
}