Spring Proxy mode

Source: Internet
Author: User
Tags throwable

1. Static Proxy

Abstract topic: Subject

 Public Interface Subject {    public  String Add ();}

Target object: Resubject

 Public class Resubject implements Subject {    public  String Add () {        System. out. println ("ok! " );         return "" ;    }}

Proxy object: Psubject

 Public classPsubject implements Subject {PrivateSubject Resubject;  PublicSubject Getresubject () {returnResubject; }     Public voidSetresubject (Subject resubject) { This. Resubject =Resubject; }     PublicString Add () {System. out. println ("Open it! "); returnResubject.add (); }}

One side

// Static proxy     @Test    publicvoid  test12 () {        Subject Subject=new Resubject ();        Psubject psubject=new  psubject ();        Psubject.setresubject (subject);        Psubject.add ();    }

2. Dynamic Agent

Interface

 Public Interface Iuserdao {    public  String Add ();      Public String edit ();}

Class

 Public classUserdaoimpl implements iuserdao{ PublicString Add () {System. out. println ("Add"); return "Add"; }     PublicString edit () {System. out. println ("Edit"); return "Edit"; }}

One side

//Dynamic Agent@Test Public voidtest13 () {final Iuserdao dao=NewUserdaoimpl (); Iuserdao pp= (Iuserdao) proxy.newproxyinstance (Dao.getclass (). getClassLoader (), Dao.getclass (). Getinterfaces (),NewInvocationhandler () { Publicobject Invoke (Object proxy, Method method, object[] args) throws Throwable {System. out. println ("Open");               Method.invoke (Dao,args); return NULL;       }       });       Pp.add ();    Pp.edit (); }

3.cglib Dynamic Agent

 Public class UserService {    publicvoid  Delete () {        System. out. println ("OK");}    }

One side (two ways)

 /*cglib Dynamic Agent*/@Test Public voidtest14 () {Final UserService servive=NewUserService (); Enhancer Enhancer=Newenhancer ();        Enhancer.setsuperclass (Servive.getclass ()); Enhancer.setcallback (NewMethodinterceptor () { PublicObject Intercept (Object o, Method, object[] objects, Methodproxy methodproxy) throws Throwable { System. out. println ("Open");                Methodproxy.invoke (servive,objects); return NULL;       }        }); /*Enhancer.setcallback (New Org.springframework.cglib.proxy.InvocationHandler () {public object Invoke (object                O, method, object[] objects) throws Throwable {System.out.println ("open");                Method.invoke (servive,objects);            return null; }        });*/userservice AA=(UserService) enhancer.create ();    Aa.delete (); }

Spring Proxy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.