Spring Boot 开发自己的拦截器

41 篇文章 0 订阅
18 篇文章 1 订阅
	可以根据拦截器Interceptor 接口的定义开发一个属于自己的拦截器MyInterceptor。
	 public static Object newProxyInstance(ClassLoader loader, Class[] interfaces,
	  InvocationHandler h) 
	   . classLoader---类加载器; 
	   . interfaces---绑定的接口,也就是吧代理对象绑定到哪些接口下,可以试多个;
	   .InvocationHandler---绑定代理对象逻辑实现。
/**
 * -- Created with IntelliJ IDEA.
 * -- @REMARK:
 * -- @Author: tanwei
 * -- @Date: 2021/11/16/1:31 下午
 * -- @Description:
 */
public interface HelloService {
   public void sayHello(String name);
}
/**
 * -- Created with IntelliJ IDEA.
 * -- @REMARK:
 * -- @Author: tanwei
 * -- @Date: 2021/11/16/1:32 下午
 * -- @Description:
 */
public class HelloServiceImpl implements HelloService {
    @Override
    public void sayHello(String name) {
        if (Objects.isNull(name) && name.isEmpty()) {
            throw new RuntimeException("parameter is  null");
        }
        System.out.println("hello" + name);
    }
}

/**
 * -- Created with IntelliJ IDEA.
 * -- @REMARK: 拦截接口
 * -- @Author: tanwei
 * -- @Date: 2021/11/16/1:35 下午
 * -- @Description:
 */
public interface Interceptor {

    //事前方法
    public boolean before();

    //事后
    public void after();

    /**
     * 取代原有事件方法
     *
     * @param invocation --回调阐述,可以通过它的proceed 方法,回调原有事件
     * @return 原有时间返回对象
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public Object around(Invocation invocation) throws InvocationTargetException, IllegalAccessException;

    //事后返回方法,事件没有发生异常执行
    public void afterReturning();

    //事件异常方法,当时间发生异常后执行
    public void afterThrowing();

    //是否使用around 方法取代原有方法
    boolean userAround();

}

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * -- Created with IntelliJ IDEA.
 * -- @REMARK: '
 * -- @Author: tanwei
 * -- @Date: 2021/11/16/4:32 下午
 * -- @Description:
 */
public class Invocation {

    private Object[] params;
    private Method method;
    private  Object target;

    public Invocation(Object target, Method method,Object[] params ) {
        this.params = params;
        this.method = method;
        this.target = target;
    }


    public  Object proceed() throws InvocationTargetException, IllegalAccessException {
        return  method.invoke(target,params);
    }
}
import java.lang.reflect.InvocationTargetException;

/**
 * -- Created with IntelliJ IDEA.
 * -- @REMARK:
 * -- @Author: tanwei
 * -- @Date: 2021/11/16/4:34 下午
 * -- @Description:
 */
public class MyInterceptor implements Interceptor {
    @Override
    public boolean before() {

        System.out.println("before……………………");
        return true;
    }

    @Override
    public void after() {
        System.out.println("after…………………………");
    }

    @Override
    public Object around(Invocation invocation) throws InvocationTargetException, IllegalAccessException {
        System.out.println("around before……………………");
        Object object = invocation.proceed();
        System.out.println("around after…………………………");
        return object;
    }

    @Override
    public void afterReturning() {
        System.out.println("afterReturning………………");
    }

    @Override
    public void afterThrowing() {
        System.out.println("afterReturning………………");
    }

    @Override
    public boolean userAround() {
        return true;
    }
}

import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * -- Created with IntelliJ IDEA.
 * -- @REMARK:
 * -- @Author: tanwei
 * -- @Date: 2021/11/16/4:46 下午
 * -- @Description:
 */
public class ProxyBean implements InvocationHandler {

    private Object target = null;

    private Interceptor interceptor = null;


    /**
     * 绑定代理对象
     *
     * @param target      代理对象
     * @param interceptor 拦截器
     * @return 代理对象
     */
    public static Object getProxyBean(Object target, Interceptor interceptor) {
        ProxyBean proxyBean = new ProxyBean();
        proxyBean.target = target;
        proxyBean.interceptor = interceptor;
        Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), proxyBean);
        return proxy;
    }


    /**
     * 处理代理对象方法逻辑
     *
     * @param proxy  代理对象
     * @param method 当前方法
     * @param args   运行参数
     * @return 方法调用结果
     * @throws Throwable 异常
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        boolean exceptionFlag = false;
        Invocation invocation = new Invocation(target, method, args);
        this.interceptor.before();
        Object object = null;
        try {
            if (this.interceptor.userAround()) {
                object = this.interceptor.around(invocation);
            } else {
                object = method.invoke(target, args);
            }
        } catch (Exception e) {
            //产生医生
            exceptionFlag = true;
        }
        this.interceptor.after();
        if(exceptionFlag){
            this.interceptor.afterThrowing();
        }else {
            this.interceptor.afterReturning();
            return object;
        }
        return null;
    }
}

    @Test
    void contextLoads() {
        //按约定获取 proxy
        HelloService helloService = new HelloServiceImpl();
        HelloService proxy = (HelloService) ProxyBean.getProxyBean(helloService, new MyInterceptor());
        proxy.sayHello("mazi");
        System.out.println("……#name is null !!!……");
        proxy.sayHello(null);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值