Dynamic proxy in Java-dynamic proxy

Source: Internet
Author: User
Dynamic proxy is such a class: it is the class generated at runtime, you must provide a set of interfaces to it when generating it, then the class declares that it implements these interfaces. Of course, you can use the class instance as any of these interfaces. Of course, this dynamic proxy is actually a proxy, it will not do substantive work for you, you must provide a handler when generating its instance, it takes over the actual work.

Definition of dynamic Proxy: A dynamic proxy class implements a group of interfaces at runtime, so that the method calls of the interface implementation class are assigned to other classes (other interface implementation classes or any classes.It is more common. To understand dynamic proxy, we need to know what is dynamic and what is acting? First, a proxy represents a set of interface methods. Note, The proxy is InterfaceInstead of class, it is not abstract class. Secondly, The type of the proxy is determined by the bound interface., Dynamic is embodied in this.

Public interface resource {
Public void operationa ();
Public void operationb ();

Public class concreteresource implements resource {
Public void operationa (){
System. Out. println ("Operation .");
Public void operationb (){
System. Out. println ("Operation B .");

Public class dynamicproxy implements invocationhandler {
Private resource;

Public dynamicproxy (){
Resource = new concreteresource ();

Public resource create (){
Resource returnresource = NULL;
Returnresource = (Resource) proxy. newproxyinstance (resource. Class. getclassloader (), new class [] {resource. Class}, this );
Return returnresource;

Public object invoke (Object proxy, method, object [] ARGs ){
Object o = NULL;
Try {
If (method. getname (). Equals ("operationa ")){
System. Out. println ("operationa in proxy ");
} Else {
O = method. Invoke (resource, argS );
} Catch (exception e ){
E. printstacktrace ();
Return O;

Public class test {
Public static void main (string [] ARGs ){
Dynamicproxy proxy = new dynamicproxy ();
Resource resource = proxy. Create ();
Resource. operationa ();

Read the code above: this is a simple dynamic proxy example. We can see that dynamic proxy does not implement the resource interface, but it contains an instance of the resource interface implementation class. In the create Method of Dynamic proxy, by calling proxy. newproxyinstance creates a proxy, binds the proxy to the resource interface, converts the proxy explicit type to the resource interface type, and returns the result, in this way, the caller can call the interface-defined method through the proxy. Because the proxy is bound to the resource interface, method calls to the resource interface will be handled by the proxy's invoke method. The invoke method will be based on different methods, or provide a new implementation, or directly call the method to the instance of the resource interface implementation class included in the proxy for processing. As mentioned above, as a dynamic proxy, it must meet the following three conditions:
1. Implemented the invocationhandler interface to implement the invoke method defined in the interface;
2. instances that contain interface implementation classes;
3. Use the proxy. newproxyinstance method to bind the proxy to the interface.

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.