Spring principles-classic AOP

Source: Internet
Author: User

Spring principles-classic AOP

Aop is a plane-oriented programming. My understanding is to package it and set it layer by layer.

First, let's take a look at how to return the proxy of an object.

 

Package agint; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy;/*** print the log * @ author Administrator **/public class ShowLogHanlder implements InvocationHandler {/*** target Object */private Object target; /*** Proxy initialization * @ param target * @ return */public static Object initNextAngient (Object target) {return Proxy. newProxyInstance (target. getClass (). getClassLoader (), target. getClass (). getInterfaces (), new ShowLogHanlder (target);} public ShowLogHanlder (Object target) returns super({this.tar get = target ;} /*** implement your own proxy logic */@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. out. println ("computing start"); Object obj = method. invoke (target, args); System. out. println ("computing end"); return obj ;}}
The purpose of this class is to call the static initNextAngient to return the proxy of the target object.

 

How can we implement nesting of multiple proxy objects?

 

Package agint; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; public class ChackValueHanlder implements InvocationHandler {private Object target; public static Object initNextAngient (Object target) {return Proxy. newProxyInstance (target. getClass (). getClassLoader (), target. getClass (). getInterfaces (), new ChackValueHanlder (target);} public ChackValueHanlder (Object target?#this.tar get = target;} public void CheakValue (Object obj) {double value = (Double) obj; if (value <0) {System. out. println ("data must be greater than 0");} else {System. out. println ("valid data") ;}@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {for (Object obj: args) {CheakValue (obj);} try {return method. invoke (target, args);} catch (Exception e) {e. printStackTrace ();} return null ;}}


The two above are proxy objects. The nested proxy can be implemented using the following code:

 

 

ShowLogHanlder.initNextAngient(ChackValueHanlder.initNextAngient(calucateImpl))


 

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.