Java Core Learning (40) using reflection to generate a JDK dynamic agent

Source: Internet
Author: User

The Java.lang.reflect package provides a proxy class and a Invocationhandler interface for generating dynamic proxy classes and dynamic proxy objects.

First, use proxy, invocationhandler create dynamic Agent

Note here that there is also a proxy class under the java.net package, but this class is used to set up the proxy server, not to confuse it.

Proxy provides the following two ways to create dynamic proxy classes and dynamic proxy instances:

    

In fact, even if the first method is used to create the dynamic proxy class, if the program needs to create the object through the proxy class, it still needs to pass in a Invocationhandler object, that is, each system-generated proxy object has a Invocationhandler object associated with it.

 Public classProxytest { Public Static voidMain (string[] args) {Myinvocationhandler Invocationhandler=NewMyinvocationhandler (); Invocationhandler.settarget (NewHero ()); Person Hero= (person) proxy.newproxyinstance (Hero.class. getClassLoader (), Hero.class. Getinterfaces (), invocationhandler);//hero.fight ();//Hero.laugh ();Hero.sayhello ("Tom");    Hero.work (); }}Interfaceperson{voidWork (); voidSayHello (String name);}Interfacefightable{voidfight ();}classHeroImplementsperson,fightable{@Override Public voidfight () {System.out.println ("Hero Fight"); } @Override Public voidWork () {System.out.println ("Hero Work"); } @Override Public voidSayHello (String name) {System.out.println ("Hello" +name); }     Public voidlaugh () {System.out.println ("Hero Laugh"); }}classMyinvocationhandlerImplementsinvocationhandler{PrivateObject Target;  Public voidsettarget (Object target) { This. target =Target; } @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("-------the method being executed:" +method); if(Args! =NULL) {System.out.println (The following is an argument that executes the method passed in:);  for(Object Val:args) {System.out.println (val); }        }        Else{System.out.println ("The method called does not have an argument");        Method.invoke (target); }        return NULL; }}

Note that the object generated by the Java Dynamic Agent must be pointed to by the interface reference, because the dynamic agent of Java actually uses the incoming interface type to create a new class, so the hero type cannot be used to point to the created object in the code above. Can only be pointed to by the person or fightable type. Look at the source code can find this point.

Second, the AOP agent

One way to implement AOP (aspect-oriented programming) is to use a Java dynamic proxy.

AOP is actually implemented in the code above, and the System.out.println method for all proxy objects is executed before the method body is executed ("the method called does not have an argument"); It's just that the code is written in Invocationhandler, and frameworks like spring decouple this part of the code from the design pattern, but that's essentially the way it works.

Java Core Learning (40) using reflection to generate a 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.