動(dòng)態(tài)代理
使用
CGLIB
import org.springframework.cglib.proxy.*;
import java.lang.reflect.Method;
class Stu{
void say(){
System.out.println("I am a student");
}
}
public class CGLibProxy {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Stu.class);
enhancer.setCallback((MethodInterceptor) (Object o, Method method, Object[] objects, MethodProxy methodProxy) -> {
System.out.println("cglib before");
Object ret = methodProxy.invokeSuper(o,objects);
System.out.println("cglib after");
return ret;
});
Stu stu = (Stu) enhancer.create();
stu.say();
stu.toString();
}
}
JDK動(dòng)態(tài)代理
import lombok.AllArgsConstructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Person{
void say();
int eat();
}
class Student implements Person{
@Override
public void say() {
System.out.println("我是學(xué)生");
}
@Override
public int eat() {
System.out.println("I eat 3");
return 3;
}
}
@AllArgsConstructor
class DynamicProxy implements InvocationHandler {
private Person p;
public Person getProxy(){
return (Person) Proxy.newProxyInstance(p.getClass().getClassLoader(),p.getClass().getInterfaces(),this);
}
void before(){
System.out.println("before");
}
void after(){
System.out.println("after");
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// before();
System.out.println("sout");
Object ret = method.invoke(p,args);
System.out.println("sout2");
// after();
return ret;
}
}
public class TestDynamicProxy {
public static void main(String[] args) {
Person p = new Student();
DynamicProxy dynamicProxy = new DynamicProxy(p);
// (Person) Proxy.newProxyInstance(p.getClass().getClassLoader(),p.getClass().getInterfaces(),dynamicProxy);
p = dynamicProxy.getProxy();
p.say();
p.eat();
}
}
cglib
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* 被代理類
*/
class Suject {
public void request() {
System.out.println("被代理的類");
}
public void sayHello(){
System.out.println("你好,你好,你好");
// request();
}
}
class SafetyCheckCallback implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("代理前。。好乐。");
Object result = methodProxy.invokeSuper(o, objects);
System.out.println("代理后。。掺炭。");
return result;
}
}
public class CglibDynamicProxy {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Suject.class);
enhancer.setCallback(new SafetyCheckCallback());
Suject proxy = (Suject) enhancer.create();
proxy.request();
proxy.sayHello();
}
}
jdk
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @ClassName: JdkDynamicProxy
* @Description: TODO
* @Create by:
* @Date: 2022/2/15 14:42
*/
interface IUpdatable {
void update();
void sayHello();
}
class UpdatableImpl implements IUpdatable {
@Override
public void update() {
System.out.println("update UpdatableImpl.");
}
@Override
public void sayHello() {
System.out.println("你好,你好");
update();
}
}
/**
* 代理類
*/
class SafetyInvocationHandler implements InvocationHandler {
private Object target;
public SafetyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("代理前.");
Object result = method.invoke(target, args);
System.out.println("代理后.");
return result;
}
}
public class JdkDynamicProxy {
public static void main(String[] args) {
IUpdatable updatable = new UpdatableImpl();
IUpdatable proxyUpdatable = (IUpdatable) Proxy.newProxyInstance(JdkDynamicProxy.class.getClassLoader(), new Class[]{IUpdatable.class}, new SafetyInvocationHandler(updatable));
proxyUpdatable.update();
proxyUpdatable.sayHello();
}
}