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