Dynamic agent and Nanning AOP-3

zhaozj2021-02-17  49

Dynamic agent and Nanning AOP-3

Use nanning to implement Interception

Keywords: AOP Nanning Dynamic Proxy AOP Dynamic Agent

Interception is one of the features of AOP, but the name of Interception is not generated with the concept of AOP. As the name suggests, Interception is intercept. The first article of this series uses the dynamic proxy function provided by Java 1.3 to implement an example of an interception, demonstrates an intercept (Interception) before and execution in a method.

This article we use Nanning AOP to achieve the same function.

Note that Interception in AOP and the interception of normal concepts are different. Let's finalize this problem.

code show as below:

Package tcftest;

Import com.tird.nanning.aspectInstance;

Import com.tird.nanning.aspects;

Import com.TIRSEN.NANNING.INTF;

Import com.TIRSEN.NANNING.INTFIMPL;

Import com.TIRSEN.NANNING.MIXINSTANCE;

Import com.tird.nanning.nullinterceptor;

Public class tcftest {

Public tcftest () {

}

Public static void main (String [] args) {

AspectInstance aspectInstance = new aspectInstance ();

MixinInstance MixinInstance = New MixinInstance ();

MixinInstance.setInterfaceClass (intf.class);

MixinInstance.SetTarget (New Intfimpl ());

MixinInstance.Addinterceptor (New nullinterceptor ());

AspectInstance.addmixin (MixinInstance);

Object Big Imma = aspectInstance.getProxy ();

INTF INTF = (INTF) BigMomma;

INTF.CALL ();

}

}

The message interception interface is as follows:

Package com.TIRSEN.NANNING;

Import com.tird.nanning.definition.singletoninterceptor;

Public class nullinterceptor imports methodinterceptor, singletoninterceptor {

Public nullinterceptor () {

}

Public Object Invoke (Invocation Invocation) throws throwable {

System.out.println ("TCF: Befor Call ME, RIGHT?");

Return invocation.invokenext ();

}

}

The output is as follows:

TCF: BEFOR CALL ME, RIGHT?

Call ()

on:

Intf.class

Intfimpl.class

The code of the class can refer to the above, because it is very simple, it is not posted.

Null Interceptor.java's code can be found in the test package provided by Nanning.

I joined: System.out.Println ("TCF: Befor Call ME, RIGHT?"); In addition: I haven't resolved the Chinese issue of Eclipse M8, who knows, please tell me, thank you.

Pay attention to the blue code of the above code:

It can be seen that intf.call () Print our expected results before this method, successfully implemented the interception of the message. This effect is the same as the Java dynamic agent implementation of the first article of this series.

It is time to introduce the use of Java dynamic proxy and nanning to implement interception.

Interception in the AOP is allowed to dynamically specify, this dynamically specified point is Point Cut.

I believe that the readers who are familiar with AOP understand the Point Cut. And ordinary interception is not the case, is fixed in the Java program. Of course, if you can make Interception dynamically specified by design patterns, profiles, etc., then you have basically made an AOP Interception.

This is the work that Nanning has made.

Here we analyze the implementation principle of NanningAop.

Author: Tian Chunfeng

Column Address: http://www.9cbs.net/develop/author/netauthor/accesine960/

转载请注明原文地址:https://www.9cbs.com/read-30263.html

New Post(0)