Spring Proxy mode

Source: Internet
Author: User

Spring proxy mode is broadly divided into static proxy mode and dynamic proxy mode.

The difference:

Static proxy mode:
The flexibility is not strong; if there are 1000 dao; there are 1000 proxy classes; not universal

Static proxy mode:

 Package Staticproxy;  Public Interface Persondao {        publicvoid  Sava ();          Public void update ();}
 PackageStaticproxy; Public classPersondaoimplImplementsPersondao { PublicString user =NULL; Persondaoimpl () {}/**     * @returnThe user*/     PublicString GetUser () {returnuser; }    /**     * @paramuser the user to set*/     Public voidsetUser (String user) { This. user =user; } persondaoimpl (String user) { This. user =user; } @Override Public voidSava () {//TODO auto-generated Method StubSystem.out.println ("Entry method")); } @Override Public voidUpdate () {//TODO auto-generated Method Stub            }}
 PackageStaticproxy;ImportJDKProxy.impl.PersonDaoImpl;//Static proxy Classes Public classPersonproxyImplementspersondao{ PublicPersondaoimpl Persondaoimpl;  Publicpersonproxy (Persondaoimpl persondaoimpl) { This. Persondaoimpl =Persondaoimpl; } @Override Public voidSava () {//TODO auto-generated Method Stub        if(Persondaoimpl.getuser ()! =NULL) {Persondaoimpl.sava (); }} @Override Public voidUpdate () {//TODO auto-generated Method Stub            }    }

Test class:

 Package Staticproxy;  Public class Test {        publicstaticvoid  main (string[] args) {        //  Create proxy class        personproxy pro  new personproxy (new  Persondaoimpl ());        Pro.sava ();    }}

Dynamic Proxy mode:
Requirements: Target class to have implementation interface

 Package Jdkproxy; // Interface  Public Interface Persondao {        publicvoid  Sava (String name);      Public void Update (String name);      Public string GetUserName (string name);}
 PackageJdkproxy.impl;ImportJdkproxy.persondao; Public classPersondaoimplImplementsPersondao {PrivateString user =NULL; /**     * @returnThe user*/     PublicString GetUser () {returnuser; }    /**     * @paramuser the user to set*/     Public voidsetUser (String user) { This. user =user; }     PublicPersondaoimpl () {} PublicPersondaoimpl (String user) { This. user =user; } @Override Publicstring GetUserName (string name) {System.out.println ("This is the GetUserName () method"); return"Oooo"; } @Override Public voidSava (String name) {System.out.println ("This is the Save () method"); } @Override Public voidUpdate (String name) {System.out.println ("This is the update () method"); }}
 PackageJdkproxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;ImportJDKProxy.impl.PersonDaoImpl;/*** Agent Factory * *@authorAdministrator **/ Public classJdkproxyfactoryImplementsInvocationhandler {PrivateObject TargetObject; /*** Generate proxy Object * *@paramTargetObject *@return     */     PublicObject Createproxyobject (Object targetObject) { This. TargetObject =TargetObject; //Build Proxy Object//Loader: Loader for target class//interfaces: All interfaces of the target class//callback Interface Class        returnproxy.newproxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). getInterfaces (),  This); }    /*** Proxy class callback method to invoke the target class method when the callback * Call the target class method to intercept*/@Override//Surround Notifications     Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Persondaoimpl Persondaoimpl= (Persondaoimpl) This. TargetObject; Object Invoke=NULL; Try {            //.... Begin (): Pre-notification            if(Persondaoimpl.getuser ()! =NULL) {                //delegate target object Call method: The target method of the call is called the connection pointInvoke =Method.invoke (TargetObject, args); }            //... commit (): Post-Notification                    } Catch(Exception e) {//callback (): Exception notification}finally{            //Close (): Final notification        }                returninvoke; }}


 PackageJdkproxy;ImportJDKProxy.impl.PersonDaoImpl; Public classJdkproxytest { Public Static voidMain (string[] args) {//Create a dynamic agent factoryJdkproxyfactory Jdkproxy =Newjdkproxyfactory (); //to create a proxy object for the target classPersondao Persondao =(Persondao) jdkproxy. Createproxyobject (NewPersondaoimpl ("")); Persondao.update ("XXX"); }}


You can comment on the content of the article by clicking the button in the lower right corner, or follow the latest news on my blog by following the button on the bottom left. If the content of the article is helpful to you, don't forget to click on the recommended button in the bottom right corner to support it.   If you have any questions about the content of the article, you can contact me by comment or email: [Email protected] If necessary reprint, please indicate the source, thank you!!


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.