Java Dynamic Agent Proxy

Source: Internet
Author: User

 Packagecom.gz_06; Public InterfaceStudentdao { Public voidlogin ();  Public voidregist ();} Packagecom.gz_06; Public classStudentdaoimplImplementsstudentdao{@Override Public voidLogin () {System.out.println (Landing); } @Override Public voidregist () {System.out.println (Registry); }} Packagecom.gz_06; Public classStudentDaoImpl2Implementsstudentdao{@Override Public voidLogin () {System.out.println ("Permission Validation"); System.out.println (Landing); System.out.println ("Dynamic Agent"); } @Override Public voidregist () {System.out.println ("Permission Validation"); System.out.println (Registry); System.out.println ("Dynamic Agent"); }} Packagecom.gz_06; Public InterfaceUserdao { Public voidAdd ();  Public voidDelete ();  Public voidChange ();  Public voidfind ();} Packagecom.gz_06; Public classUserdaoimplImplementsuserdao{@Override Public voidAdd () {System.out.println ("Add Action"); } @Override Public voidDelete () {System.out.println ("Delete operation"); } @Override Public voidChange () {System.out.println ("Modify Action"); } @Override Public voidfind () {System.out.println ("Query Operation"); }} Packagecom.gz_06; Public classUserDaoImpl2Implementsuserdao{@Override Public voidAdd () {System.out.println ("Permission Check"); System.out.println ("Add Action"); System.out.println ("Logging"); } @Override Public voidDelete () {System.out.println ("Permission Check"); System.out.println ("Delete operation"); System.out.println ("Logging"); } @Override Public voidChange () {System.out.println ("Permission Check"); System.out.println ("Modify Action"); System.out.println ("Logging"); } @Override Public voidfind () {System.out.println ("Permission Check"); System.out.println ("Query Operation"); System.out.println ("Logging"); }} Packagecom.gz_06;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Proxy; Public classTest { Public Static voidMain (string[] args) {Userdao ud=NewUserdaoimpl ();        Ud.add ();        Ud.delete ();        Ud.change ();        Ud.find (); System.out.println ("----------"); //However, in the actual development, generally in the operation of the time required to verify the permissions, the operation needs to log records, how to do? //because it is a subset of people who need permission validation, and logging, the first way is to rewrite an implementation classUserdao ud2=NewUserDaoImpl2 ();        Ud2.add ();        Ud2.delete ();        Ud2.change ();        Ud2.find (); System.out.println ("--------"); //Other classes also require permission validation, and do so? Studentdao stu=NewStudentdaoimpl ();        Stu.login ();        Stu.regist (); System.out.println ("-----"); Studentdao STU2=NewStudentDaoImpl2 ();        Stu2.login ();        Stu2.regist (); System.out.println ("------"); //This is disgusting, so we have to learn to be lazy, we need to use dynamic agents/*static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) returns a specified interface for the        An instance of the proxy class that can assign a method call to the specified invocation handler.          Invocationhandler is the interface that is implemented by the calling handler for the proxy instance. Object Invoke (Object proxy, Method method, object[] args) processes the method call on the proxy instance and returns the result. */Invocationhandler Handler=NewMyinvocationhandler (UD); Userdao Proxy=(Userdao) proxy.newproxyinstance (Ud.getclass (). getClassLoader (), Ud.getclass (). Getinterfaces (), handler);        Proxy.add ();        Proxy.delete ();        Proxy.change ();                Proxy.find (); System.out.println ("------"); Invocationhandler Handler2=NewMyinvocationhandler (STU); Studentdao Proxy2=(Studentdao) proxy.newproxyinstance (Stu.getclass (). getClassLoader (), Stu.getclass (). Getinterfaces (), Handler2        );        Proxy2.login ();            Proxy2.regist (); }} Packagecom.gz_06;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classMyinvocationhandlerImplementsInvocationhandler {PrivateObject Target;//Target proxy Object     PublicMyinvocationhandler (Object target) { This. target=Target; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Permission Check"); Object result=Method.invoke (target, args); System.out.println ("Logging"); returnresult; }}
View Code

Java Dynamic Agent Proxy

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.