1. 静态代理
public interface HouseServiceInterface {
public void rentHouse();
}
/**
* @author muzi@softeem.com
* @description 房东
* @since 2021/4/10 19:37
*/
public class HouseService implements HouseServiceInterface{
@Override
public void rentHouse(){
System.out.println("我出租了一套房");
}
}
/**
* @author muzi@softeem.com
* @description 一级中介
* @since 2021/4/10 19:38
*/
public class HouseServiceProxy implements HouseServiceInterface{
private HouseServiceInterface houseService;
public HouseServiceProxy(HouseServiceInterface houseService) {
this.houseService = houseService;
}
@Override
public void rentHouse(){
System.out.println("请交中介费");
houseService.rentHouse();
System.out.println("租期到了,请续费");
}
}
public class App3 {
public static void main(String[] args){
HouseServiceInterface houseServiceProxy = new HouseServiceProxy(new HouseService());
houseServiceProxy.rentHouse();
}
}
2. 动态代理
2.1. JDK动态代理
基于接口实现
/**
* @author muzi@softeem.com
* @description 基于JDK的动态代理
* InvocationHandler是jdk提供的在反射包中的类,作用是在JDK动态代理中对目标方法
进行增强
* @since 2021/4/10 20:19
*/
public class MyJDKProxy implements InvocationHandler {
//目标对象
private Object target;
public MyJDKProxy(Object target) {
this.target = target;
}
/**
*
* @param proxy 代理类对象
* @param method 目标对象中的方法
* @param args 目标对象中的方法的参数
* @return 目标方法执行后的返回值
* @throws Throwable 目标方法抛出的异常
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("请交中介费");
Object result = method.invoke(target, args);//Object result =
joinPoint.proceed();
System.out.println("租期到了,请续费");
return result;
}
}
/**
* @author muzi@softeem.com
* @description
* @since 2021/4/10 20:34
*/
public class JDKDynamicProxy {
public static void main(String[] args) {
//目标对象
HouseServiceInterface houseService = new HouseService();
MyJDKProxy dynamicProxy = new MyJDKProxy(houseService);
HouseServiceInterface proxyInstance = (HouseServiceInterface)Proxy.newProxyInstance(houseService.getClass().getClassLoader(),houseService.getClass().getInterfaces(),dynamicProxy);
proxyInstance.rentHouse();
}
}
2. 2. CGLIB动态代理
基于继承实现(目标类被 final 修饰)
<!-- cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
public class MyCglibProxy implements MethodInterceptor {
/**
*
* @param obj 目标对象
* @param method 拦截方法
* @param args 目标方法的参数
* @param methodProxy 目标方法
* @return
* @throws Throwable
*/
@Override
public Object intercept(Object obj, Method method, Object[] args,MethodProxy methodProxy) throws Throwable {
System.out.println("请交中介费===CGLIB");
Object result = methodProxy.invokeSuper(obj, args);
System.out.println("租期到了,请续费===CGLIB");
return result;
}
}
public class CGLIBDynamicProxy {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
//设置父类(目标对象所属类)Class
enhancer.setSuperclass(HouseService.class);
//设置回调器(方法拦截处理器)
enhancer.setCallback(new MyCglibProxy());
//创建代理对象
HouseService HouseServiceProxy =(HouseService)enhancer.create();
HouseServiceProxy.rentHouse();
}
}