Examples of implementing dynamic proxies with spring AOP

Source: Internet
Author: User

Let me show you examples of implementing dynamic proxies with spring AOP (computer printing)

Here's a look at the specific code:

Define the interface of a printer first

1  Package Aop007_comprint; 2 3  Public Interface Print {4     5      Public void Colorprint ();    // Color Printing 6      Public void Whiteprint ();    // Black and white printing 7 }

Then define two implementation classes for color printing and black-and-white printing, respectively

1  PackageAop007_comprint;2 3  Public classColorprintImplementsPrint {4 5 @Override6      Public voidColorprint () {7System.out.println ("[core business logic] I am a color printer! ");8System.out.println ("[core business logic] I am mainly responsible for printing color information!" ");9     }Ten  One @Override A      Public voidWhiteprint () { -         //System.out.println ("[core business logic] I am mainly responsible for printing black and white materials!" "); -     } the  -}
1  PackageAop007_comprint;2 3  Public classWhiteprintImplementsPrint {4 5 @Override6      Public voidWhiteprint () {7System.out.println ("[core business logic] I am a black and white printer! ");8System.out.println ("[core business logic] I am mainly responsible for printing black and white materials!" ");9     }Ten      One @Override A      Public voidColorprint () { -         //System.out.println ("[core business logic] I am mainly responsible for printing color information!" "); -     } the  -}

Define a proxy class Printhandler implement dynamic Agent printing function

1  PackageAop007_comprint;2 3 ImportJava.lang.reflect.InvocationHandler;4 ImportJava.lang.reflect.Method;5 Importjava.util.Date;6 7  Public classPrinthandlerImplementsInvocationhandler {8     //target type is not deterministic9     PrivateObject Target;Ten      PublicPrinthandler (Object target) { One          This. target =Target; A     } -     /* - * Return returns the content returned by the original target method is the method to be executed the      */ - @Override -      PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable { - before (); +          -         //Specific business logic code +         //Object returnprintobj = Targer.method//used to interpret the following line of code AObject returnprintobj =Method.invoke (target, args); at          - After (); -         returnreturnprintobj; -     } -      -     Private voidbefore () { in         //predecessor Task -System.out.println (NewDate ()); toSYSTEM.OUT.PRINTLN ("printer"); +System.out.println ("Southern It Academy offers!") "); -     } the      *     Private voidAfter () { $         //Post TaskPanax NotoginsengSystem.out.println ("College Printer"); -System.out.println ("Please take good care, broken you can afford!!! "); the     } +  A}

Finally, define a test class Testprint function for testing printing

1  PackageAop007_comprint;2 3 ImportJava.lang.reflect.InvocationHandler;4 ImportJava.lang.reflect.Proxy;5 6 /*7 * Add a "Dynamic proxy class", similar to a star broker8 * Separating the code of the core business logic from the non-core9 * Give the non-core code to the broker to manage,Ten * Note: Brokers and want stars must implement the same interface One  */ A  Public classTestprint { -  -      Public Static voidMain (string[] args) { the         //First step: Create an instance of the target implementation class -Print Cprint =Newcolorprint (); -Print Wprint =Newwhiteprint (); -          +         //Step Two: Create a dynamic proxy class (CEO) -Invocationhandler Cprinthandler =NewPrinthandler (cprint); +Invocationhandler Wprinthandler =NewPrinthandler (wprint); A          at         //Step Three: Create a dynamic proxy (like a static proxy, the declared variable is still the target's interface) -Print Cprintproxy =(Print) proxy.newproxyinstance (Cprint.getclass (). getClassLoader (), - Cprint.getclass (). Getinterfaces (), - Cprinthandler); -Print Wprintproxy =(Print) proxy.newproxyinstance (Wprint.getclass (). getClassLoader (), - Wprint.getclass (). Getinterfaces (), in Wprinthandler); -          toCprintproxy.colorprint ();//contrast cprint.colorprint (); [Core code only] after running the difference +System.out.println ("------------------------------"); -Wprintproxy.whiteprint ();//contrast wprint.whiteprint (); [Core code only] after running the difference the     } *  $}

This is the end of the run, and the result is as follows:

Examples of implementing dynamic proxies with spring 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.