First, dynamic agent introduction
Package Cn2;public interface Iuserdao {/** * Login method */public void login ();/** * Registration method */public void register (); Package Cn2;public class Userdaoimpl implements Iuserdao {@Overridepublic void login () {System.out.println ("User Login");} @Overridepublic void Register () {System.out.println ("User Registration");}} Package Cn2;public class Test {public static void main (string[] args) {Iuserdao dao = new Userdaoimpl ();d ao.register ();d AO . Login ();}}
User Registration
User Login
If I want to add permission checksum logging to each method, then the code is implemented as follows:
package cn2;public interface iuserdao {/** * Login Method */public void Login ();/** * registration Method */public void register ();} package cn2;public class userdaoimpl implements iuserdao {@Overridepublic Void login () {system.out.println ("User login"); @Overridepublic void register () {system.out.println ("User Registration");}} Package cn2;public class userdaoimpl2 implements iuserdao {private void actioncheck () {System.out.println ("permission Check");} Private void record () {System.out.println ("Logging");} @Overridepublic void login () {this.actioncheck (); SYSTEM.OUT.PRINTLN ("login"); This.record ();} @Overridepublic void register () {this.actioncheck (); SYSTEM.OUT.PRINTLN ("registration"); This.record ();}} Package cn2;public class test {public static void main (String[] args) {iuserdao dao = new usERDAOIMPL2 ();d ao.register ();d ao.login ();}}
Permission check
Registered
Log records
Permission check
Login
Log records
Why write a single class to implement the interface, and increase the permissions check and logging? Because we are designing, we need to ensure the development of the extended function, the principle of changing the code closure. I have defined a new class to implement the interface, that is, to extend the functionality of the original interface, rather than modify it on the original implementation class.
However, if all this design, is a very scary thing, if the need to change, then we need to define a lot of implementation classes, too complex. What to do?
In life, we will encounter a lot of problems, such as: I want to rent a house, but I do not know the landlord, how to do? Find an intermediary. The same truth, I do not want to modify the code to increase the permissions check and logging functions, then I find a proxy object to help me achieve it?
Dynamic Proxy: This is the object that is generated during the program's operation.
In Java, a proxy class and a Invocationhandler interface are provided under the Java.lang.reflect package, and dynamic proxy objects can be generated by using this class and interface. The JDK only proxies for interfaces.
A method in the proxy class creates a dynamic proxy object
public static Object newproxyinstance (ClassLoader loader,class<?>[] Interfaces,invocationhandler h)
The method that will eventually call Invocationhandler
object invoke (object proxy,method method,object[] args)
package cn2;public interface iuserdao {/** * Login Method */public void Login ();/** * registration Method */public void register ();} package cn2;public class userdaoimpl implements iuserdao {@Overridepublic Void login () {system.out.println ("User login"); @Overridepublic void register () {system.out.println ("User Registration");}} Package cn2;import java.lang.reflect.invocationhandler;import java.lang.reflect.method;public class MyInvocationHandler implements InvocationHandler {private Object Target;public myinvocationhandler (Object target) {this.target = target;} /** * returns the dynamic proxy object */@Overridepublic object invoke (Object proxy, method method, object[] args) throws throwable {system.out.println ("permission Check"); object invoke = method.invoke (Target, args); System.out.println("Logging"); return invoke;}} Package cn2;import java.lang.reflect.proxy;public class test {public static void main (String[] args) {//use dynamic proxy before Iuserdao dao = new userdaoimpl (); Dao.register ();d ao.login (); System.out.println ("-------------------");//iuserdao proxy = (Iuserdao) after using dynamic agent Proxy.newproxyinstance (Dao.getclass (). getClassLoader (), dao.getclass (). Getinterfaces (), new Myinvocationhandler (dao));p roxy.register ();p roxy.login ();}}
User Registration
User Login
-------------------
Permission check
User Registration
Log records
Permission check
User Login
Log records
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1878267
Dynamic agent of Java