35.1 Dynamic Proxy
- Features: Bytecode is created as you use it, and loaded as you use it
- Function: Enhance the method without modifying the source code
- classification:
- Dynamic proxy based on interface
- Involved class: Proxy
- Provider: JDK official
- How to create a proxy object:
- Use the newProxyInstance method in the Proxy class
- Requirements for creating proxy objects:
- The proxy class implements at least one interface, if not, it cannot be used
- Parameters of the newProxyInstance method:
- ClassLoader : class loader
- It is used to load the proxy object bytecode. Use the same class loader as the proxy object. Fixed writing.
- ***Class[]***: bytecode array
- It is used to make the proxy object and the proxy object have the same method. Fixed writing.
- InvocationHandler : used to provide enhanced code
- It is let us write how to proxy. We are generally an implementation class of this interface, usually anonymous inner classes, but it is not necessary.
- The implementation class of this interface is written by whoever uses it.
package com.itheima.proxy;
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() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object returnValue = null;
Float money = (Float)args[0];
if("saleProduct".equals(method.getName())) {
returnValue = method.invoke(producer, money*0.8f);
}
return returnValue;
}
});
proxyProducer.saleProduct(10000f);
}
}
- Dynamic proxy based on subclasses:
- Involved class : Enhancer
- Provider : third-party cglib library
- How to create a proxy object:
- Use the create method in the Enhancer class
- Requirements for creating proxy objects:
- The proxy class cannot be the final class
- Parameters of the create method:
- Class : Bytecode, it is the bytecode used to specify the proxy object.
- Callback : used to provide enhanced code
- It is let us write how to proxy. We are generally an implementation class of this interface, usually anonymous inner classes, but it is not necessary.
- The implementation class of this interface is written by whoever uses it.
- We generally write the sub-interface implementation class of this interface: MethodInterceptor
package com.itheima.cglib;
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 cglibProducer = (Producer)Enhancer.create(producer.getClass(), new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object returnValue = null;
Float money = (Float)args[0];
if("saleProduct".equals(method.getName())) {
returnValue = method.invoke(producer, money*0.8f);
}
return returnValue;
}
});
cglibProducer.saleProduct(12000f);
}
}