Interpretation and demonstration of dynamic Proxy interface for reflection in Java language

Source: Internet
Author: User

Interpretation and demonstration of dynamic Proxy interface for reflection in Java language

Java in the JDK1.3 introduced the dynamic agent mechanism, can be used in the framework programming and platform programming time to capture events, audit data, logs and other functions realized, first look at the UML diagram of design patterns:


When you invoke an interface API, the actual implementation class inherits the interface, and the invocation is implemented by proxy.

The two key interface classes implemented by dynamic proxies in Java are as follows:

Java.lang.reflect.Proxy

Java.lang.reflect.InvocationHandler

We will implement the dynamic agent process through the Invocationhandler interface, creating a proxy interface.

A proxy class, and then test the complete program. The following steps are required to implement demo demos:

One: First define our own Pojo object interface class Iexample and Iuser

Package Com.example.pojo;public interface Iexample {public void SetName (string name);p ublic String getName ();p ublic void Setdesc (string description);p ublic string GetDesc ();}
Package Com.example.pojo;public interface Iuser {public void Setuserid (string userID);p ublic string GetUserID ();p ublic void Setusername (String userName);p ublic string GetUserName ();}
Two: The realization of our own Invocationhandler interface, where map I used to store the data of the Pojo object, the advantage is that the Pojo interface does not need to create the implementation class, only the interface can be defined by the proxy directly using the class, which is very useful in real project development.

Package Com.example.reflection;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Java.util.hashmap;import Java.util.map;public class Myproxyview implements Invocationhandler {private Map<object, object> Map = null;public static Object newinstance (class[] interfaces) {RET Urn Proxy.newproxyinstance (MyProxyView.class.getClassLoader (), interfaces, new Myproxyview ());} Private Myproxyview () {this.map = new hashmap<object, object> ();} @Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {object result = NULL; String methodName = Method.getname (), if (Methodname.startswith ("get")) {string name = Methodname.substring ( Methodname.indexof ("get") + 3); return map.get (name);} else if (Methodname.startswith ("set")) {String name = methodname.substring (Methodname.indexof ("set") + 3); Map.put (name , Args[0]); return null;} else if (Methodname.startswith ("is")) {String name = methodname.substring (methoDname.indexof ("is") + 2); return (Map.get (name));} return result;}}

Third: The proxy method is used to initialize the agent to get the Pojo object, Run and test:

Package Com.example.reflection;import Com.example.pojo.iexample;import Com.example.pojo.iuser;public class textproxy {public static void main (string[] args) {Iexample example = (iexample) myproxyview.newinstance (New class[]{ Iexample.class}); Iuser user = (Iuser) myproxyview.newinstance (New Class[]{iuser.class});//Aduit Bean 1example.setname ("my Example"); Example.setdesc ("My proxy example");//Aduit Bean 2user.setuserid ("jia20003"); User.setusername (" Gloomyfish "); System.out.println ("exmaple name:" + example.getname ()); System.out.println ("exmaple desc:" + example.getdesc ()); System.out.println (); System.out.println ("User ID:" + User.getuserid ()); System.out.println ("User name:" + user.getusername ());}}
Four: The result of the operation is as follows:

Exmaple name:my exampleexmaple desc:my proxy exampleuser id:jia20003user name:gloomyfish

The Java dynamic Proxy approach is important for framework programming, whether on a web or desktop side

And the real technology is the spring framework.

Interpretation and demonstration of dynamic Proxy interface for reflection in Java language

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.