The mechanism of the Java JDK Dynamic agent

Source: Internet
Author: User

A: Preface

I saw a veiled remark on the internet when I secured some of the features of spring. "With interfaces, Spring AOP will default through the dynamic proxy of the JDK to implement proxy classes, and spring AOP will use Cglib to implement proxy classes when the interface is not applicable," I do not know the dynamic agent mechanism of the JDK, learning Java is just know will use, will go to check the API documents on the line, so the bottom of the operation of the way I am not very clear, now that you want to understand some of the underlying code, thanks to their own thinking, most of them are similar.

Two: Realize

First, the code for the interface and implementation class is given:

Userdao.java:

 Package Dynamicproxy;  Public Interface Userdao {    string Saveuser (string name);     void deleteuser (String name);}

Userdaoimpl.java:

 package   Dynamicproxy;  public  class  Userdaoimpl implements   userdao{@Override 
    public   string Saveuser (string name) {Syste        M.out.println ( "Name:" +name);     return   name; } @Override  public  void   DeleteUser (String name) {System.out.println ( "name to delete:" +name

Now that the JDK is the dynamic agent, then we need some tired to implement the "Java.lang.reflect.InvocationHandler" interface, which has the Invoke (*,*,*) method, the method has three parameters, The parameter meaning below will say the following code:

 PackageDynamicproxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classMousehandlerImplementsInvocationhandler {PrivateObject Target;  PublicMousehandler (Object target) { This. target =Target; }     Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("The class of the agent is:" +Proxy.getclass ()); System.out.println ("Target class:" +target.getclass () + "target object--->" +target); System.out.println ("Method:" +method); System.out.println ("Parameter:" +args); System.out.println ("Start output Log"); Object obj=Method.invoke (target, args); System.out.println ("Value of obj:" +obj); returnobj; }}

When you print out all of these parameters yourself, you will know the meaning of the parameters. I wrote it down down here.

Object Proxy: The class to be proxied (the class that prints the result to------> proxy is: Classes $Proxy 0)

Method method: This means the methods to be called (Printing results--------> method: Public abstract java.lang.String DynamicProxy.UserDao.saveUser ( java.lang.String))

Object[] args: Here is the parameter array, which is the parameter of the method

There is this method (public object invoke (object proxy, Method method, object[] args)) and finally we return the value: The value returned is the value we call the method, the method returns (the output is---- Value of >obj: mouse)

Again, this and object proxy, if you are in Method.invoke (Proxy,args) Here the first parameter still use proxy (auto-generated proxy class), in the Run program you will find that Cheng will run (error), the loop, the error code is as follows:

Here you need to specify the object to be proxied.

Now look at the Test.java class

 PackageDynamicproxy;ImportJava.lang.reflect.Proxy; Public classTest { Public Static voidMain (String args[]) {Userdao Userdao=NewUserdaoimpl (); System.out.println ("-" +Userdao.getclass (). getClassLoader ());//(Printed result:-->[email protected]) System.out.println ("-" +Userdao.getclass (). Getinterfaces ());//(printed result:-->[ljava.lang.class;@61de33) Userdao userdaoproxy=(Userdao) Proxy. Newproxyinstance (Userdao.getclass (). getClassLoader (), Userdao.getclass (). Getinterfaces () , NewMousehandler (Userdao)); Userdaoproxy.saveuser (Mouse); }        }

See the three parameters in the proxy (*,*,*) method

Userdao.getclass (). getClassLoader (): This is the ClassLoader
Userdao.getclass (). Getinterfaces (): interface of the agent
New Mousehandler (Userdao): Instantiate the Incoming object (reference to Userdaoimpl) that needs to be proxied

If you add a breakpoint debugging, you will find that in fact, the Invoke method in Mousehandler only in the call method Userdaoproxy.saveuser ("Mouse"), the time will be executed, that is, the call is Saveuser ("mouse") , in effect, when invoking the Invoke method. The above knowledge actually has to look at the information on the Internet, in combination down write, but really need their own breakpoints to run under
In fact, now I understand the underlying code, many of them need to be knocked over, their own breakpoints to run, their own to debug, only so their understanding will be more thorough. It's been a few days to understand the spring IOC and AOP, and the IOC is fine, but AOP itself feels depressed to understand. But I still try to understand, it is good for myself. Tomorrow is my own birthday, this is my social (not graduating from the society) the first birthday, write a blog in advance to wish their birthday happy. Internship has been almost 3 months, do not dare to say that they are now how much of a good, but really feel that their progress, they have a sense of direction, they know what needs to learn something. This is very important. Although I know to learn Java before, but only understand the fur, the real thing still do not understand. This is the foundation. Recently in the tutorial a lot of things, Java, Linux, computer networks and so on. There's still a long way to go. I'm going to write a three-month-old feeling about it tomorrow. Keep a record of your feelings. Efforts to refuel, day-to-day progress. Efforts!!!

The mechanism of the Java JDK Dynamic agent

Related Article

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.