Dynamic proxy for application of Java reflection mechanism

Source: Internet
Author: User
Tags reflection

1. Static proxy class and dynamic proxy class differences

static proxy : The proxy class and the proxy class are required to implement the corresponding set of interfaces at the same time, through the proxy class to invoke the method of overriding the interface, is actually executed by the proxy class of the same

The invocation of the method.

Dynamic proxy : dynamically creates a proxy class, based on the proxy class and the interfaces it implements, while the program is running. when invoking an abstract method of the implementation of the proxy class, a call to the same method as the proxy class is initiated.

The technical points involved:

① provides an implementation class for the Invocationhandler interface and overrides its Invoke () method

②proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), h);

2. Advantages of dynamic agents

It can generate proxy objects dynamically for any class that wants to be proxied when the program is run (the proxy object contains the common agent logic written), it does not care about what the proxy class is, and it can be completely detached from the class being proxied, thus flexibly applied to different scenarios.

The following specific code to reflect the benefits of dynamic agents

The first one is the code for the static proxy

package reflection mechanism;
Static proxy features are classes of proxy classes and target objects that are determined during compilation
Not conducive to the extension of the program. At the same time, each proxy class can serve only one interface, which
Program development will inevitably produce too many agents
Interface
Interface clothproduct{
void Productcloth ();
}

By proxy class
Class Nikeclothfactory implements clothproduct{

@Override
public void Productcloth () {
SYSTEM.OUT.PRINTLN ("Nike produces a batch of clothes");

}

}
proxy class
Class Proxyfactory implements clothproduct{
Clothproduct CF;
When you create an object of a proxy class, you actually pass in an object of the proxy class
Public proxyfactory (Clothproduct CF) {
THIS.CF=CF;
}
@Override
public void Productcloth () {

SYSTEM.OUT.PRINTLN ("Agent class starts execution");
Cf.productcloth ();
}

}
public class Jingtaidaili {
public static void Main (string[] args) {
Nikeclothfactory nc=new nikeclothfactory ();
Proxyfactory proxy=new Proxyfactory (NC);
Proxy.productcloth ();
}
}
Each proxy class must establish a proxy class, then multiple proxy classes will have to write multiple proxy classes too cumbersome

The following is a dynamic proxy class

package reflection mechanism;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;

A dynamic proxy is a method by which a customer invokes another object through a proxy class.
And to dynamically create a proxy object for the target class as needed when the program is running

Interface subject{
void action ();
}

By proxy class
Class Realsubject implements subject{
public void Action ()
{
System.out.println ("I am a proxy class");
}
}

Class Myivocationhandler implements invocationhandler{
Object obj;//Declaration of objects that implement an interface's proxy class
Instantiates an object of a proxy class, returning an object of a proxy class
public object Blind (object obj) {
This.obj=obj;

Return Proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), this);
}
When you initiate a call to an overridden method through an object of the proxy class, it is converted to a call to the following Invoke method
@Override
public object Invoke (object arg0, Method arg1, object[] arg2) throws Throwable {
Object obj1=arg1.invoke (obj, arg2);

return obj1;
}
}
public class Dongtaidaili {
public static void Main (string[] args) {
Object of the class being proxied
Realsubject rs=new realsubject ();
2 Creating an object that implements a class that implements the Ivocationhandler interface
Myivocationhandler mh=new Myivocationhandler ();
3 Call the Blind () method to dynamically return an object of the proxy class that implements the interface of the same class as the real subject
Object Obj=mh.blind (RS);
Subject sub= (Subject) obj;//at this point the sub is the proxy class object
Sub.action ();//Go to the Call of the Invoke () method of the implementation class of the Ivocationhandler interface
System.out.println ();
proxy class using static proxy to embody dynamic proxy
Nikeclothfactory nick=new nikeclothfactory ();
Clothproduct cp=new clothproduct ();
Clothproduct cp= (clothproduct) mh.blind (Nick);
Cp.productcloth ();
}
}

All of the proxy classes correspond to a proxy class, and when an object from the proxy class initiates an invocation of the overridden method, it is converted to a call to the Invoke method of the only proxy class, and the different proxy classes correspond to different methods, so the Invoke method is dynamically variable and facilitates the proxy for multiple proxy classes.

Dynamic proxy for application of Java reflection mechanism

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.