Java Dynamic proxy (AOP)

Source: Internet
Author: User

There are two types of proxies: one is the Java Dynamic proxy that implements the interface, and the other is the proxy that does not implement the interface based on cglib.

 

Proxy provides a static method for creating dynamic proxy classes and dynamic proxy objects. It is also the parent class of all dynamic proxies,

The following describes dynamic proxy through a simple example:

Dog:

Public interface dog {
Public void Info ();
Public void run ();
}

 

Dog implementation class:

Public class dogimpl implements dog {

Public void Info (){

System. Out. println ("I Am a hound ");
}

Public void run (){
System. Out. println ("I am running very fast ");

}
}

 

Public class dogutil {

Public void Method1 (){
System. Out. println ("=========== simulate the first common method =========== ");
}
Public void method2 (){
System. Out. println ("=========== simulate the second common method =========== ");
}
}

 

/**
* This class is an implementation class of invocationhandler. The invoke method of this class will be implemented as a proxy object.
*/
Public class myinvocationhandler implements invocationhandler {

/** Objects to be proxy */
Private object target;
 
Public void settarget (object target ){
This.tar get = target;
}

/** All methods for executing the dynamic proxy object will be replaced with the following invoke Method */
Public object invoke (Object proxy, method, object [] ARGs)
Throws throwable {
Dogutil = new dogutil ();
Dogutil. Method1 ();

Object result = method. Invoke (target, argS );

Dogutil. method2 ();
Return result;
}

}

 

/**
* A dynamic proxy instance is generated for the specified target.
*/
Public class myproxyfactory {
// Note that proxy provides two methods to create a dynamic proxy class and a dynamic proxy instance, for example:
 
Public static object getproxyinstance (object target ){
Myinvocationhandler handler = new myinvocationhandler ();
Handler. settarget (target );
/* Create and return the dynamic proxy */
Return proxy. newproxyinstance (target. getclass (). getclassloader (),
Target. getclass (). getinterfaces (), Handler );
}
Public static object getproxyclass (object target) throws exception {
Myinvocationhandler handler = new myinvocationhandler ();
Handler. settarget (target );
/* Create and return the dynamic proxy */
Class clazz = proxy. getproxyclass (target. getclass (). getclassloader (), target. getclass (). getinterfaces ());
Constructor con = clazz. getconstructor (new class [] {invocationhandler. Class });
Return con. newinstance (new object [] {handler });
}
}

 

Test:

Public class test {

Public static void main (string [] ARGs) throws exception {
Dog target = new dogimpl ();
// Dog = (DOG) myproxyfactory. getproxyclass (target );
Dog dog = (DOG) myproxyfactory. getproxyinstance (target );
Dog.info ();
Dog. Run ();
}
}

 

Output result:

=========== Simulate the first common method ===============
I am a hound
=========== Simulate the second common method ==============
=========== Simulate the first common method ===============
I ran very fast.
=========== Simulate the second common method ==============

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.