Java Dynamic Proxy example with main comment

Source: Internet
Author: User
Tags object object

Java Proxy is based on reflection and only supports interface-based dynamic proxies.

Java Dynamic agents are the foundation of all architectures and must be understood. Say less nonsense, first on the code to get perceptual knowledge.

The sample code has the main comment.

Interface:

Public interface Subject {
string Hello (string name);
void Say ();
}

Interface implementation:
public class Impsubject implements Subject {
@Override
public string Hello (string name) {
Return String.Format ("Hello%s", name);
}

@Override
public void Say () {
System.out.println (This.getclass (). GetName ());
}
}

Dynamic Proxy method Invocation interface:
Invocationhandler is the callback interface that auto-generated proxy classes automatically trigger when calling methods. There is only one Invocke method that can do all sorts of things in this way, such as filtering, intercepting, and so on.
 public class Myinvocationhandler implements Invocationhandler {
Private Subject Subject;
Public Myinvocationhandler (Subject Subject) {
This.subject = Subject;
The
//object is a proxy class strength, method is called the proxy strength methods, Args is the method parameter (no parameter is null)
//Can be delegated to the agent's strength object, or can not be delegated to the proxy object.
@Override
Public Object Invoke (Object object, method, Object [] args) throws throwable{

System.out.println (Method.getname ());
System.out.println (Object.getclass (). GetName ());
System.out.println (Method.invoke (subject, args));
return arrays.tostring (args);
}
}

Client test code:
public class Main {

public static void Main (string[] args) {
Subject Subject = new Impsubject ();
Invocationhandler Invocationhandler = new Myinvocationhandler (subject);

The principle is: according to the first parameter ClassLoader load automatically generated proxy class, proxy class will automatically generate the second parameter provides all the interface, when the proxy class method is called, the third parameter Invocationhandler interface is invoked to implement the proxy.
Subject proxy = (Subject) proxy.newproxyinstance (Subject.getclass (). getClassLoader (), New class<?>[]{ Subject.class}, Invocationhandler);
System.out.println (Proxy.hello ("Java"));
System.out.println (Proxy.getclass (). GetName ());

Proxy.say ();
}
}

Output:

Hello
Com.sun.proxy. $Proxy 0
Hello Java
[Java]
Com.sun.proxy. $Proxy 0
Say
Com.sun.proxy. $Proxy 0
Proxy. Impsubject
Null

Java Dynamic Proxy example with main comment

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.