静态/动态代理

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();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值