Dynamic Agent and Nanning AOP-2 Implement Introduction

zhaozj2021-02-17  48

Dynamic agent and Nanning AOP-2

Use nanning to implement Introduction

Keywords: AOP Nanning Dynamic Proxy AOP Dynamic Agent

Introduction is one of the greatest features of AOP programming from OO programming. The simplest, intuitive Introduction is to add new fields, methods, etc. to existing classes.

In both methods can be implemented in both methods:

1, use XML configuration files

2, use Java code

AOP implemented using dynamic proxy has some "restrictions":

1. You need to implement the AOP function, you must define the interface, which is the requirements of the Java dynamic agent;

2. Because it is the AOP of the interface, Interception and Introduction can only be implemented in public methods, and cannot be implemented in private methods;

3, Nanning AOP does not implement Interception for this level of attribute field.

Let's first see the implementation of the second method:

code show as below:

Package tcftest;

Import com.tird.nanning.aspectInstance;

Import com.TIRSEN.NANNING.INTF;

Import com.TIRSEN.NANNING.INTFIMPL;

Import com.TIRSEN.NANNING.MIXINSTANCE;

Import com.tird.nanning.nullinterceptor;

Import com.tird.nanning.definition.AaspectClass;

Import com.tird.nanning.definition.AaspectDefinition;

Public class introtest {

Public static void main (String [] args) {

AspectClass aspectClass = new aspectClass ();

aspectClass.setInterface (intf.class);

AspectClass.Addinterceptor (MockInterceptor.class);

AspectClass.Addinterceptor (nullinterceptor.class);

AspectClass.SetTarget (intfimpl.class);

AspectDefinition aspectDefinition = new aspectDefinition ();

AspectDefinition.setInterface (Sideaspect.class);

askDEFINITION.ADDInterceptor (nullinterceptor.class);

AspectDefinition.Addintercept ;.class;

AspectDefinition.SetTarget (SideaspectImpl.class);

AspectClass.Addaspect (aspectDefinition);

Object Bigmomma1 = aspectClass.newinstance ();

(INTF) BIGMMMA1) .CALL ();

(Sideaspect) bigmomma1). SideCall ();

}

}

com.TIRSEN.NANNING.MIXININSTANCE $ INVOCAPL

Call ()

com.TIRSEN.NANNING.MIXININSTANCE $ INVOCAPL

Side: SideCall

It can be seen that objects obtained by aspectClass can implement two interfaces of intf and SideASpect, and complete the functions of calling their respective implementations. The above: intf.class and sideaspect.class are excuses

IntFimpl.class and SideaspectImpl.class are implemented to interfaces.

Everyone can realize their interfaces and classes themselves to complete this test, the code does not paste it here.

Using XML to implement the code of the Introduction is as follows:

1, XML configuration file:

Class = "tcftest.mockinterceptor"

Scope = "singleton" />

Interface = "tcftest.ihelloworld"

Target = "tcftest.helloworldimpl" />

2, code part

Package tcftest;

Import java.io.ioException;

Import com.tird.nanning.aspectInstance;

Import com.tird.nanning.aspects;

Import com.tird.nanning.config.aspectSystem;

Import com.tird.nanning.xml.aspectSystemParser;

Public class polo imports iPolo {

Public static void main (string [] args) throws oException {

AspectSystemParser AspectSystemParser

= New aspectSystemParser ();

AspectSystem aspectSystem

= aspectSystemParser.Parse

Polo.class.getResource ("nanning.xml");

Ipolo o =

(IPolo) aspectSystem.newinstance (iPolo.class);

AspectInstance Instance

= Aspects.getaspectInstance (O);

Ihelloworld HelloWorld

= (Ihelloworld) instance.getproxy ();

HelloWorld.helloworld ();

}

}

The operation is as follows:

com.TIRSEN.NANNING.MIXININSTANCE $ INVOCAPL

HelloWorldImpl 's HelloWorld Method

Because Eclipse M8 has Chinese issues, all English is output.

Here are our analysis of the source code of Nanning to further understand the implementation process of the AOP.

My progress benefits: the introduction of the programmer.

Author: Tian Chunfeng

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

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

New Post(0)