動態代理兩種實現方法

1.概述

在spring 開源框架中,很多情況下使用了動態代理技術,比如 FEIGN, MYBATIS 的Mapper接口,還有比如spring 的事務管理。

2. 動態代理實現

動態代理實現技術有兩種方式。

1.使用jdk的動態代理
只適用在有接口的情況下
2.使用CGLIB

  • 支持接口代理
  • 只是無接口代理

2.1 使用接口代理的方式

使用代碼實現:

  • 接口定義
public interface IUserService {

    String addUser(String name);
}
  • 實現類定義
public class UserService  implements IUserService{
    @Override
    public String addUser(String name) {
        System.err.println("添加用戶:" +name);
        return "user:" + name ;
    }
}
  • 動態代理實現
public class DynamicProxy  {
    public static <T> T  createProxy(T servie){
        T proxy = (T) Proxy.newProxyInstance(DynamicProxy.class.getClassLoader(),
                servie.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.err.println("前置執行。。。");
                method.invoke(servie,args);
                System.err.println("後置執行。。。");

                return null;
            }
        });
        return proxy;
    }
}
  • 代理執行
public class JdkProxyDemo {
    public static void main(String[] args) {
        IUserService iUserService=new UserService();
        IUserService proxy= DynamicProxy.createProxy(iUserService);
        proxy.addUser("老王");
    }
}

2.2 使用GCLIB的方式

  • 引入cglib 包
<dependency>
             <groupId>cglib</groupId>
             <artifactId>cglib</artifactId>
             <version>2.2.2</version>
</dependency>
  • 被代理類
public class UserService {
    String addUser(String name) {
        System.err.println(name);
        return name;
    }
}
  • 代理增強類
public class ServiceInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.err.println("前置執行...");
        Object result = methodProxy.invokeSuper(obj, objects);
        System.err.println("後置執行...");
        return result;
    }
}
  • 實現代理
public class ProxyDemo {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserService.class);
        enhancer.setCallback(new ServiceInterceptor());
        UserService userService = (UserService) enhancer.create();
        userService.addUser("老王");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章