Implement proxy invocationhandler using Java Dynamic proxy

Source: Internet
Author: User

The dynamic proxy I just learned about Java is implemented using the java. Lang. Reflect. invocationhandler class and the java. Lang. Reflect. invocationhandler interface. It's easy to use. Let's record it and share it with you.

Use your own proxy to call the method of the proxy object, and you can add your own business logic (AOP) before and after running the method, such as the log and test method running time.

The proxy object must be a class object that implements an interface;

Your own proxy implements the invocationhandler interface and implements your own business in the invoke method of this interface.

 

The interface implemented by the proxy object here is userdao. Java

 

Package COM. proxy. dao; import COM. proxy. model. user; public interface userdao {/*** save * @ Param u */Public void saveuser (User U);/*** Delete */Public void deleteuser ();}

The class of the proxy object is userdaoimpl. java. We need to add some logic before running the above method.

Package COM. proxy. dao. impl; import COM. proxy. dao. userdao; import COM. proxy. model. user; public class userdaoimpl implements userdao {@ overridepublic void saveuser (User U) {system. out. println ("saved by staff ~~ ") ;}@ Overridepublic void deleteuser () {system. Out. println (" delete a person ~~ ");}}

 

Your own proxy object loghandler. Java needs to implement the java. Lang. Reflect. invocationhandler Interface

Define your business logic in the invoke method.

Package COM. proxy. myproxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; public class loghandler implements invocationhandler {/** proxy object */private object O; Public loghandler (Object O) {super (); this. O = O;}/*** start business * @ Param Str */private void beforemethod (string Str) {system. out. println ("method" + STR + "() Start ~~ ");}/*** End Service * @ Param Str */private void aftermethod (string Str) {system. out. println ("method" + STR + "() end ~~ ") ;}@ Overridepublic object invoke (Object arg0, method arg1, object [] arg2) throws throwable {// your own business beforemethod (arg1.getname ()); // The proxy object business is arg1.invoke (O, arg2) through reflection; // your own business aftermethod (arg1.getname (); return NULL ;}}

 

You must call the newproxyinstance method of the Java. Lang. Reflect. proxy class to obtain your own proxy object,This method has three parameters:

1. The loader for obtaining this class object is the same as the object to be proxy. It can be Ud. getclass (). getclassloader () here ()

2. The interface implemented by the proxy object is Ud. getclass (). getinterfaces ()

3. the user-defined business object here is ih.

Package COM. proxy. main; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. proxy; import COM. proxy. dao. userdao; import COM. proxy. dao. impl. userdaoimpl; import COM. proxy. model. user; import COM. proxy. myproxy. loghandler; public class testmain {public static void main (string [] ARGs) {// The proxy object userdao UD = new userdaoimpl (); // combined with the aspect business invocationhandler ih = new loghandler (UD); // generate our own proxy object to implement other aspect business userdao userproxy = (userdao) proxy. newproxyinstance (ud. getclass (). getclassloader (), Ud. getclass (). getinterfaces (), IH); userproxy. saveuser (new user (); system. out. println ("************************"); userproxy. deleteuser ();}}

 

The running result is

Method saveuser () Start ~~ Save personnel ~~ Method saveuser () end ~~ * *********************** Deleteuser () method start ~~ Delete A person ~~ Method deleteuser () end ~~

 

Source code: http://download.csdn.net/detail/best198706/5817375

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.