zoukankan      html  css  js  c++  java
  • java一、jdk动态代理

    JDK静态代理

      代理类中,指定了需要代理的对象,已经固定好了,所以为静态代理

    public class StaticProxy {

    interface UserService{
    void addUser();
    }

    static class UserServiceImpl implements UserService{
    @Override
    public void addUser() {
    System.err.println("新增用户001");
    }
    }

    static class Proxy implements UserService{

    UserServiceImpl userImpl;

    Proxy(UserServiceImpl userImpl){
    this.userImpl=userImpl;
    }

    @Override
    public void addUser() {
    System.err.println("开始代理,新增用户");
    userImpl.addUser();
    System.err.println("结束代理,新增成功");
    }
    }

    public static void main(String[] args) {
    UserServiceImpl userServiceImpl=new UserServiceImpl();
    Proxy proxy=new Proxy(userServiceImpl);
    proxy.addUser();
    }
    }

    JDK动态代理-解耦,代码量减少

      通过反射的方式,获取代理对象,无需固定代理类,即动态代理

    public class DynamicProxy {

    interface UserService{
    void addUser();
    }

    static class UserServiceImpl implements UserService{
    @Override
    public void addUser() {
    System.err.println("新增用户001");
    }
    }

    public static void main(String[] args) {
    UserService userService = (UserService) Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(), new Class[]{UserService.class}, new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Object invoke = method.invoke(new UserServiceImpl(), args);
    return invoke;
    }
    });
    userService.addUser();
    }
    }

     核心方法说明:根据接口,通过反射获取类对象;(所以jdk动态代理必须要有接口)

    public static Object newProxyInstance(ClassLoader loader,
                                              Class<?>[] interfaces,
                                              InvocationHandler h)
    {
        final Class<?>[] intfs = interfaces.clone(); 
        Class<?> cl = getProxyClass0(loader, intfs);
        final Constructor<?> cons = cl.getConstructor(constructorParams);
    }

     

    Cglib动态代理

      需实现MethodInterceptor接口,实现代理

    import org.springframework.cglib.proxy.Enhancer;
    import org.springframework.cglib.proxy.MethodInterceptor;
    import org.springframework.cglib.proxy.MethodProxy;
    
    import java.lang.reflect.Method;
    
    public class CglibDynamicProxy {
        
        static class UserServiceImpl {
    
            public void addUser() {
                System.err.println("新增用户001");
            }
        }
    
        static class CglibProxy implements MethodInterceptor{
    
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                methodProxy.invokeSuper(o,objects);
                return o;
            }
        }
    
        public static void main(String[] args) {
            Enhancer enhancer=new Enhancer();
            enhancer.setSuperclass(UserServiceImpl.class);//设置代理类
            enhancer.setCallback(new CglibProxy());//设置回调方法
            UserServiceImpl userServiceImpl= (UserServiceImpl) enhancer.create();
            userServiceImpl.addUser();
        }
    }

     不同处

     jdk动态代理

      Proxy.newProxyInstance()生成代理类

      被代理类需要实现接口

      InvocationHandler()执行代理方法

     cglib动态代理  

      enhancer.create() 生成代理类

      代理的是类对象,不需要接口

      代理类需要实现MethodInterceptor接口

     
  • 相关阅读:
    UtraEdit 不启用语法彩色显示
    XXX 不是当前用户的有效责任,请联系您的系统管理员
    子帐 SLA(SubLedger Accounting)
    518 vue组件的data是函数,不是对象
    517 vue注册组件语法糖,模板的分离写法
    516 vue父组件和子组件
    515 vue全局组件和局部组件
    514 vue组件化开发概述,注册组件的基本步骤
    513 v-model:表单绑定,原理,radio,checkbox,select,修饰符,值绑定
    512 高阶函数 filter、map、reduce
  • 原文地址:https://www.cnblogs.com/ruerror/p/14283482.html
Copyright © 2011-2022 走看看