zoukankan      html  css  js  c++  java
  • Spring代理模式

    1.静态代理

     抽象主题 :Subject 

    public interface Subject {
        public String add();
    }

    目标对象:ReSubject 

    public class ReSubject implements Subject {
        public String add() {
            System.out.println("ok!");
            return "";
        }
    }

     代理对象:Psubject 

    public class Psubject implements Subject {
    
        private Subject reSubject;
    
        public Subject getReSubject() {
            return reSubject;
        }
    
        public void setReSubject(Subject reSubject) {
            this.reSubject = reSubject;
        }
    
        public String add() {
            System.out.println("开启!");
            return reSubject.add();
        }
    }

    单侧   

     //静态代理
        @Test
        public void test12(){
            Subject subject=new ReSubject();
            Psubject psubject=new Psubject();
            psubject.setReSubject(subject);
            psubject.add();
        }

    2.动态代理

    接口

    public interface IUserDAO {
        public String add();
        public String edit();
    }

    public class UserDAOImpl implements IUserDAO{
        public String add() {
            System.out.println("add");
            return "add";
        }
    
        public String edit() {
            System.out.println("edit");
            return "edit";
        }
    }

    单侧

    //动态代理
        @Test
        public void test13(){
           final IUserDAO dao=new UserDAOImpl();
           IUserDAO pp=(IUserDAO) Proxy.newProxyInstance(dao.getClass().getClassLoader(), dao.getClass().getInterfaces(), new InvocationHandler() {
               public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                   System.out.println("开启");
                   method.invoke(dao,args);
                   return null;
               }
           });
           pp.add();
           pp.edit();
        }

    3.cglib动态代理

    public class Userservice {
        public void delete(){
            System.out.println("ok");
        }
    }

    单侧(两种方式)

     /*cglib动态代理*/
        @Test
        public void test14(){
            final Userservice servive=new Userservice();
            Enhancer enhancer=new Enhancer();
            enhancer.setSuperclass(servive.getClass());
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    System.out.println("开启");
                    methodProxy.invoke(servive,objects);
                    return null;
                }
            });
           /* enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
                public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                    System.out.println("开启");
                    method.invoke(servive,objects);
                    return null;
                }
            });*/
            Userservice aa =(Userservice) enhancer.create();
            aa.delete();
        }
  • 相关阅读:
    无题
    1.1tensorflow2.0 张量
    某某大肠_tidb_集群创建用户
    某某大肠_替换TiDB 3.0集群的tidb-server命令工具
    某某大肠_配置spark的thriftserver模块
    date_and_time
    SpringBoot整合Mybatis
    SpringBoot(3)Thymeleaf使用详解
    SpringBoot(2)实现CRUD
    SpringBoot(1)入门篇
  • 原文地址:https://www.cnblogs.com/qjt970518--/p/7263157.html
Copyright © 2011-2022 走看看