Java Dynamic Agent

Source: Internet
Author: User

Java Dynamic Agent parsing (i) principle

Dynamic proxy simply means that when we want to invoke the method of an object, we usually call the object's own method through the object, but sometimes we can not get directly to the object, but can only get a "proxy" object, through the proxy object to proxy the real object, call the real object method. So the question comes, why can't we just get the object? Proxy object Agent Real object and what's the use of it? In fact, the proxy object to proxy (processing) The real object, is to call the real object before and after the method to do some processing. Java has implemented dynamic proxies, implemented through Java.lang.reflect.InvocationHandler, Java.lang.reflect.Method, and java.lang.reflect.Proxy three classes.


ImplementTo implement a dynamic proxy, you define an interface, define a generic object to implement the class, and then customize a proxy processor implementationInvocationhandler, implement the agent inside.
Package com.liujun.dynamicproxy;/** * A common interface * @author ASUS * */public interface Studenti {public void doing ();}

Package com.liujun.dynamicproxy;/** * Generic Object implementation class * @author ASUS * */public class Realstudent implements Studenti {@Overridep ublic void doing () {System.out.println ("I am a Student");}}

Package Com.liujun.dynamicproxy;import Java.lang.reflect.invocationhandler;import java.lang.reflect.method;/** * Proxy Call processor * @author ASUS * */public class Proxyhandler implements Invocationhandler {//Real objects to be proxied private object realobject;pub Lic Proxyhandler (Object realobject) {this.realobject=realobject;} @Overridepublic object Invoke (Object object, method method, object[] arg) throws Throwable {Object result;// Method call before Beforedoing ();//Call Real object method Result=method.invoke (This.realobject, arg);//The result of the dispatch method represents the parameter of the object obj arg// Afterdoing after method invocation (); return result;} /** * Proxy object method call before calling the method */private void Beforedoing () {System.out.println ("beforedoing ()");} /** * Proxy object method call after calling the method */private void Afterdoing () {System.out.println ("afterdoing ()");}}

Package Com.liujun.test;import Java.lang.reflect.proxy;import Com.liujun.dynamicproxy.proxyhandler;import Com.liujun.dynamicproxy.realstudent;import Com.liujun.dynamicproxy.studenti;public class DynamicProxyTest {public static void Main (string[] args) {//Real object Studenti student=new realstudent ();//Agent Processor Proxyhandler Proxyhandler=new Proxyhandler (student);//Generate Proxy Objects Studenti proxystuent= (studenti) proxy.newproxyinstance (Student.getclass () through the proxy processor. getClassLoader (), Student.getclass (). Getinterfaces (), proxyhandler);//The proxy object proxy invokes the Real object method Proxystuent.doing ();}}







Java Dynamic Agent

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.