动态代理相比静态代理(23种涉及模式之一)优势在于:动态生产代理类,减少编译期静态代理类的数量
1.jdk动态代理:
被代理的对象必须是接口
package jdkProxy;
public interface People {
void speak();
void eat();
}
package jdkProxy;
public class Chinese implements People{
@Override
public void speak() {
System.out.println("说中文");
}
@Override
public void eat() {
System.out.println("吃中餐");
}
}
package jdkProxy;
public class English implements People{
@Override
public void speak() {
System.out.println("说英文");
}
@Override
public void eat() {
System.out.println("吃西餐");
}
}
package jdkProxy;
public interface TrafficMachine {
void run();
}
package jdkProxy;
public class Car implements TrafficMachine{
@Override
public void run() {
System.out.println("在地上跑");
}
}
package jdkProxy;
public class Plane implements TrafficMachine{
@Override
public void run() {
System.out.println("在天上飞");
}
}
package jdkProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DynamicProxyInvocationHandler implements InvocationHandler{
private Object o;
public Object getO() {
return o;
}
public void setO(Object o) {
this.o = o;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object invoke=method.invoke(o, args);
System.out.println("代理附加内容");
return invoke;
}
}
package jdkProxy;
import java.lang.reflect.Proxy;
public class dynamicProxyTest {
public static void main(String[] args) {
DynamicProxyInvocationHandler dynamicProxyInvocationHandler=new DynamicProxyInvocationHandler();
People chinese=new Chinese();
dynamicProxyInvocationHandler.setO(chinese);
People chineseProxy = (People) Proxy.newProxyInstance(chinese.getClass().getClassLoader(),
chinese.getClass().getInterfaces(), dynamicProxyInvocationHandler);
chineseProxy.speak();
chineseProxy.eat();
People english=new English();
dynamicProxyInvocationHandler.setO(english);
People englishProxy=(People)Proxy.newProxyInstance(english.getClass().getClassLoader(),
english.getClass().getInterfaces(), dynamicProxyInvocationHandler);
englishProxy.speak();
englishProxy.eat();
TrafficMachine car=new Car();
dynamicProxyInvocationHandler.setO(car);
TrafficMachine carProxy=(TrafficMachine)Proxy.newProxyInstance(car.getClass().getClassLoader(),
car.getClass().getInterfaces(), dynamicProxyInvocationHandler);
carProxy.run();
TrafficMachine plane=new Plane();
dynamicProxyInvocationHandler.setO(plane);
TrafficMachine planeProxy=(TrafficMachine)Proxy.newProxyInstance(plane.getClass().getClassLoader(),
plane.getClass().getInterfaces(), dynamicProxyInvocationHandler);
planeProxy.run();
}
}
打印结果:
说中文
代理附加内容
吃中餐
代理附加内容
说英文
代理附加内容
吃西餐
代理附加内容
在地上跑
代理附加内容
在天上飞
代理附加内容
2.cglib动态代理:
被代理的对象可以为接口和不被final修饰的类
package cglibProxy;
public class Chinese {
public void speak() {
System.out.println("说中文");
}
public void eat() {
System.out.println("吃中餐");
}
}
package cglibProxy;
public class English {
public void speak() {
System.out.println("说英文");
}
public void eat() {
System.out.println("吃西餐");
}
}
package cglibProxy;
public class Car {
public void run() {
System.out.println("cglib在地上跑");
}
}
package cglibProxy;
public class Plane {
public void fly() {
System.out.println("cglib在天上飞");
}
}
package cglibProxy;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CglibProxy implements MethodInterceptor{
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
Object intercept = arg3.invokeSuper(arg0, arg2);
System.out.println("代理附加");
return intercept;
}
}
package cglibProxy;
import net.sf.cglib.proxy.Enhancer;
public class CglibProxyTest {
public static void main(String[] args) {
CglibProxy cglibProxy=new CglibProxy();
Enhancer chineseEnhancer=new Enhancer();
chineseEnhancer.setSuperclass(Chinese.class);
chineseEnhancer.setCallback(cglibProxy);
Chinese chinese= (Chinese)chineseEnhancer.create();
chinese.speak();
chinese.eat();
Enhancer englishEnhancer=new Enhancer();
englishEnhancer.setSuperclass(English.class);
englishEnhancer.setCallback(cglibProxy);
English english= (English)englishEnhancer.create();
english.speak();
english.eat();
Enhancer carEnhancer=new Enhancer();
carEnhancer.setSuperclass(Car.class);
carEnhancer.setCallback(cglibProxy);
Car car=(Car)carEnhancer.create();
car.run();
Enhancer palneEnhancer=new Enhancer();
palneEnhancer.setSuperclass(Plane.class);
palneEnhancer.setCallback(cglibProxy);
Plane plane=(Plane)palneEnhancer.create();
plane.fly();
}
}
打印结果:
说中文
代理附加
吃中餐
代理附加
说英文
代理附加
吃西餐
代理附加
cglib在地上跑
代理附加
cglib在天上飞
代理附加