Java Dynamic proxy Implementation Method

Source: Internet
Author: User

The current Java development package includes support for dynamic proxies, but its implementation only supports the implementation of interfaces.
The main implementation is through the java. Lang. Reflect. proxy class and Java. Lang. Reflect. invocationhandler interface.
 
Examda prompt:
The proxy class is mainly used to obtain dynamic proxy objects. The invocationhandler interface is used to constrain the implementation of callers. The following describes the business side defined by the helloworld interface.
Helloworldimpl is the implementation of the helloworld interface, and helloworldhandler is
Invocationhandler interface implementation. The Code is as follows:
  Business interface:

Public interface helloworld {
Void sayhelloworld ();
}
  Business interface implementation:

Public class helloworldimpl implements helloworld {
Public void sayhelloworld (){
System. Out. println ("Hello world! ");
}
}
To implement invocationhandler, you need to add a processing job before and after the interface method call. Here, only two strings are output to the background before and after the method call. The Code is as follows:
Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. method;
Public class helloworldhandler implements invocationhandler {
// Original object to be proxy
Private object objoriginal;
/**
* Constructor.
* @ Param OBJ examda: the original object to be proxy.
*/
Public helloworldhandler (Object OBJ ){
This. objoriginal = OBJ;
}
Public object invoke (Object proxy, method, object [] ARGs)
Throws throwable {
Object result;
// Before method call
Dobefore ();
// Call the method of the original object
Result = method. Invoke (this. objoriginal, argS );
// After method call
Doafter ();
Return result;
}
Private void dobefore (){
System. Out. println ("before method invoke! ");
}
Private void doafter (){
System. Out. println ("after method invoke! ");
}
}
  Test code:

Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. proxy;
Public class test {
Public static void main (string [] ARGs ){
Helloworld hW = new helloworldimpl ();
Invocationhandler handler = new helloworldhandler (HW );
Helloworld proxy = (helloworld) proxy. newproxyinstance (
HW. getclass (). getclassloader (),
HW. getclass (). getinterfaces (),
Handler );
Proxy. sayhelloworld ();
}
}
First, obtain the implementation object of a business interface;
Obtain an invocationhandler implementation, which is the helloworldhandler object;
Create a dynamic proxy object;
Ø call the sayhelloworld () method through a dynamic proxy object. At this time, two strings are output before and after the original object helloworldimpl. sayhelloworld () method.
  The output of the running test class is as follows:

Before method invoke!
Hello world!
After method invoke!
There are many method call codes in the test class. In our actual application, we can use the configuration file to simplify the client call implementation. In addition, you can use dynamic proxies to implement simple AOP.

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.