码迷,mamicode.com
首页 > 编程语言 > 详细

Spring代理模式

时间:2017-08-20 10:18:18      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:tin   psu   edit   return   title   turn   delete   framework   odi   

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();
    }
技术分享

Spring代理模式

标签:tin   psu   edit   return   title   turn   delete   framework   odi   

原文地址:http://www.cnblogs.com/s1297-lgy/p/7398791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!