Java Proxy JDK Dynamic proxy application case column _java

Source: Internet
Author: User
Java Proxy has JDK dynamic proxy, cglib agent, here only the JDK dynamic proxy, JDK dynamic proxy mainly uses the Java reflection mechanism (both Java.lang.reflect package)
The principle is (for example, singers and brokers):
Establish a common interface, such as: Singer public interface Singer;
With specific classes to implement the interface, such as: Jay Chou, he is a singer so realize Singer this class, class Mysinger implements Singer
Create a proxy class, this is the broker, he needs to implement the Invocationhandler class and rewrite the Invoke method
So when there is something, to find Jay Chou (specific category), you must first to the broker (agent) there to deal with the agent in the decision to do not meet you (this method to do)
1, singer Interface
Copy Code code as follows:

Public interface Singer {

public abstract void Sing ();

Public abstract String s ();
}

2, the specific singer
Copy Code code as follows:

public class Mysinger implements Singer {
public void Sing () {
TODO auto-generated Method Stub
System.err.println ("Singing ...)." ");
}
}

3, proxy class (broker)
Copy Code code as follows:

Public class Agent implements invocationhandler{
public Object target;

Binding
public object bind (Object target) {
This.target=target;
Proxy must be put back
Return Proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), this);
}

Re -
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
Object o =null;
System.out.println ("Start business");
SYSTEM.OUT.PRINTLN ("Judge Authority");

o = Method.invoke (target, args);//Execution method

System.out.println ("End of Transaction");
return o;
}
}

4, testing (why the interface can be, below you will find that the proxy return is their interface class, which want a proxy class, you can proxy multiple classes, as long as the class is the same interface is implemented)
Copy Code code as follows:

public class Test {
public static void Main (string[] args) {
//
Agent a =new agent ();
Singer s= (Singer) a.bind (New Mysinger ());
S.sing ();
}
}

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.