带你了解代理模式,linux系统管理技术手册百度网盘

public class StaticProxyService implements PayService {

private PayService payService;

public StaticProxyService(PayService payService) {

this.payService = payService;

}

@Override

public void pay() {

before();

payService.pay();

after();

}

public void before(){

System.err.println(“before执行方法前执行”);

}

public void after(){

System.err.println(“after执行方法后执行”);

}

}

Main方法测试

/**

  • @Auther: heng

  • @Date: 2021/4/25 11:14

  • @Description: ProxyMain

  • @Version 1.0.0

*/

public class ProxyMain {

public static void main(String[] args) {

PayService payService = new StaticProxyService(new PayServiceImpl());

payService.pay();

}

}

JDK动态代理方法实现需求(被代理类必须实现接口 否则会报未实现接口错误)


需要实现InvocationHandler

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

/**

  • @Auther: heng

  • @Date: 2021/4/25 13:11

  • @Description: JdkInvocationHandler jdk动态代理

  • @Version 1.0.0

*/

public class PayInvocationHandler implements InvocationHandler {

public Object targerClass;

public PayInvocationHandler(Object targerClass) {

this.targerClass = targerClass;

}

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

before();

Object result = method.invoke(targerClass, args);

after();

return result;

}

public void before(){

System.err.println(“before执行方法前执行”);

}

public void after(){

System.err.println(“after执行方法后执行”);

}

}

创建JdkProxy方法

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Proxy;

/**

  • @Auther: heng

  • @Date: 2021/4/25 13:20

  • @Description: JdkProxy jdk动态代理类 执行

  • @Version 1.0.0

*/

public class JdkProxy {

public static T newProxyInstance(T targerClass, InvocationHandler invocationHandler){

ClassLoader classLoader = targerClass.getClass().getClassLoader();

Class<?>[] interfaces = targerClass.getClass().getInterfaces();

return (T) Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);

}

}

Main测试代码

//jdk动态代理

PayService payService = new PayServiceImpl();

PayInvocationHandler payInvocationHandler = new PayInvocationHandler(payService);

PayService proxyPayService = JdkProxy.newProxyInstance(payService, payInvocationHandler);

proxyPayService.pay();

Cglib动态代理实现需求方法(可以代理实现接口的类和不实现接口的类)


需要引入cglib jar

cglib

cglib

3.2.9

需要实现MethodInterceptor

创建CglibInvocationHandler

import net.sf.cglib.proxy.MethodInterceptor;

import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**

  • @Auther: heng

  • @Date: 2021/4/25 13:37

  • @Description: CglibInvocationHandler cglib动态代理

  • @Version 1.0.0

*/

public class CglibInvocationHandler implements MethodInterceptor {

@Override

public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

before();

Object result = methodProxy.invokeSuper(o, args);

after();

return result;

}

public void before(){

System.err.println(“before执行方法前执行”);

}

public void after(){

System.err.println(“after执行方法后执行”);

}

}

创建CglibProxy动态代理执行类

import net.sf.cglib.proxy.Enhancer;

import net.sf.cglib.proxy.MethodInterceptor;

/**

  • @Auther: heng

  • @Date: 2021/4/25 13:42

  • @Description: CglibProxy cglib动态代理执行类

  • @Version 1.0.0

*/

public class CglibProxy {

public static T newProxyInstance(T targerClass, MethodInterceptor methodInterceptor){

return (T) Enhancer.create(targerClass.getClass(),methodInterceptor);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值