springboot-cglib动态代理实现代码增强

cglib动态代理demo

通过动态代理实现方法拦截,可以:

  1. 返回特定值;
  2. 实现特定逻辑;
  3. 前后逻辑增强;
import cn.cloudwalk.entity.TServiceInfo;
import cn.cloudwalk.service.TServiceInfoService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.List;

/**
 * (TServiceInfo)表控制层
 *
 * @author makejava
 * @since 2021-02-02 15:34:01
 */
@RestController
@RequestMapping("tServiceInfo")
public class TServiceInfoController {
    /**
     * 服务对象
     */
    @Resource
    private TServiceInfoService tServiceInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TServiceInfo selectOne(Integer id) {
        //使用cglib增强器创建代理类对象
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.tServiceInfoService.getClass());
        enhancer.setCallback(new MyProxy());
        TServiceInfoService proxy = (TServiceInfoService) enhancer.create();
        //使用代理类对象调用方法
        TServiceInfo tServiceInfo = proxy.queryById(id);
        System.out.println("代理类调用被拦截的方法:"+ JSONObject.toJSONString(tServiceInfo));
        List<TServiceInfo> tServiceInfos = proxy.queryAllByLimit(0, 10);
        System.out.println("代理类调用其他方法:"+tServiceInfos);
        return this.tServiceInfoService.queryById(id);
    }

    /**
     * 方法拦截器,配合cglib代理使用
     */
    private static class MyProxy implements MethodInterceptor {

        @Override
        public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

            if (!(obj instanceof TServiceInfoService)) {
                //调用者不是预想的,放行
                return methodProxy.invokeSuper(obj, objects);
            }
            if ("queryById".equals(methodProxy.getSignature().getName())) {
                //拦截这个方法,并返回结果给调用者
                System.out.println("拦截到了");
                TServiceInfo tServiceInfo = new TServiceInfo();
                tServiceInfo.setServiceName("demo");
                tServiceInfo.setId(234);
                //直接返回结果给调用者
                return tServiceInfo;
            }
            //放行前后逻辑
            System.out.println("Before:" + methodProxy.getSignature().getName());
            //放行
            Object o = methodProxy.invokeSuper(obj, objects);
            System.out.println("After:" + methodProxy.getSignature().getName());
            return o;
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值