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:
Jdk:proxy
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; }}
Test:
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