The concept of dynamic agents
A dynamic agent is a program that automatically creates a proxy object in place of the proxy object to perform the corresponding operation, for example, we have a user in a project that has been put into operation DAO class Userdao is used to make database additions and deletions to the user object, but one day, Requirements in the user's additions and deletions to record the corresponding log, this is how to do? We go directly to modify the source code of Userdao, and then in each method of Userdao to add logging function, which is obviously unreasonable, it violates the Java OCP principle, that is, to modify the closure of the expansion open. For example, change the existing code as follows:
Interface class
Public interface Iuserdao {public void Add (user user);p ublic user load (int id);p ublic void Delete (int id);p ublic void Upda Te (user user);}
Implementation Class
public class Userdao implements Iuserdao {public void Add (user user) {System.out.println ("user added:" + user);} Public User load (int id) {System.out.println ("Load User, id=" + ID); return null;} public void Delete (int id) {System.out.println ("deleted user, id=" + ID);} @Overridepublic void Update (user user) {System.out.println ("Updated User:" + user);}}
Business class Interface
Public interface Iuserservice {public void Add (user user);p ublic void Delete (int id);p ublic User load (int id);p ublic void Update (user user);
Business Class implementation Class
public class UserService implements Iuserservice {Iuserdao userdao;public Iuserdao Getuserdao () {return userdao;} public void Setuserdao (Iuserdao userdao) {This.userdao = Userdao;} public void Add (user user) {userdao.add (user);} public void Delete (int id) {userdao.delete (id);} Public User load (int id) {return userdao.load (ID);} @Overridepublic void Update (user user) {userdao.update (user);}}
One day we're Userdao. Each method executes before logging, and we define a logger class to specifically output the log:
public class Logger {public static void log (String info) {System.out.println (info);}}
also implements a proxy class:
1, write a class inherit Invocationhandlerpublic class Logproxy implements Invocationhandler {//3, create the proxy object private Object target;//3, Create a proxy object, the parameter is the object to be proxied, the return value is the proxy object public static object GetInstance (Object o) {logproxy proxy = new Logproxy ();p roxy.target = O;ob ject result = Proxy.newproxyinstance (O.getclass (). getClassLoader (), O.getclass (). Getinterfaces (), Proxy); return Result;} @Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {if ( Method.isannotationpresent (Loginfo.class)) {//check if there are loginf annotations on this method loginfo Li = method.getannotation (loginfo.class); Gets the annotation Logger.log (new Date (). toString () + "--->" + li.value ()); Get direct value, output log}object o = Method.invoke (target, args); return o;}
Custom annotation are as follows:
@Retention (retentionpolicy.runtime) public @interface loginfo {public String value () Default "";}
When it's all over, at this point, if we want to log before the corresponding method executes, we just need to use the Loginfo annotation in the method of Iuserdao to join the log, and when the method executes, it will automatically join the logging function, as follows:
Public interface Iuserdao {@LogInfo ("Add a user") public void Add (user user);p ublic user load (int id); @LogInfo ("Delete a US ER ") public void Delete (int id), @LogInfo (" Update a user ") public void Update (user user);
Only the method with @loginfo annotations is used to log logs, such as add,delete,update, and load does not log logs, so the annotations allow you to flexibly control which methods you want to log
Use
public static void Main (string[] args) {Iuserdao Userdao = new Userdao (); Iuserdao userdaoproxy = (Iuserdao) logproxy.getin Stance (Userdao); UserService userservice = new UserService () Userservice.setuserdao (userdaoproxy); Userservice.add (new User ());}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Dynamic Agent in detail, with dynamic agents and annotations to implement the logging function