Introducing AOP in Nanowar

xiaoxiao2021-03-06  15

Through NanoWar's Nanowar and its Struts plug-in implementation of the IOC mode transparency, but cannot introduce Nanoaop in the standard Nanowar to be a regret. After reading the Nanocontainer code, I decided to add AOP support to Nanowar. To support AOP in components and instances, first introduce Interceptor Chains. Can consider introducing in XML definitions

'InterceptorName' PointCuts = '

Filter '/> label. This tag and component or instance of the tag at the same level. Where interceptor points out the name of the interceptor. The implementation of the interceptor can be registered in the same configuration file through standard component. PointCuts is a filter expression of the intercepting method (the regular expression of Perl5, and several filter rules predefined by NanoAop).

To implement this tag in Nanowar, you need to modify ScopedCotainerComposer, XmlContainerBuilder, ContainerRecorder, ServletContainerListener, the defaultlifecyclecontainerbuider class. Since the interceptor cannot obtain an instance during IOC container initialization. Therefore, there is also a need to introduce a new universal interceptordeLegate as a true interceptor instance and a calling bridge that needs to be intercepted. Since the amount of source code is large, the index is introduced into the new class in front of the original class, but in addition to the AOP section, other remains unchanged.

ScopedcotainerComposer => aspectablescopedcontainerComposer.java

Public aspectablescopedcontainerComposer () throws classnotfoundexception {

This (New DynaopaspectablePicOContainerFactory (). CreateContainer ());

}

XmlContainerBuilder => aspectableXmlcontainerbuilder.java

Protected PicoContainer CreateContainerfromscript

....

AspectablePicocontainerFactory ContamericFactory = New DynaopaspectablePicOContainerFactory ();

AspectablePicoContainer ChildContainer = ContainerFactory

.CreateContainer (ComponentadapterFactory, ParentContainer);

Cuts = ChildContainer.getPointCutsFactory ();

....

}

Private Void RegisterComponentImplementation (NanoContainer Container,

Element Element) Throws ClassNotFoundException,

Malformedurlexception {

......

Map Interceptors = CREATEINTERCEPTORS (ELEMENT);

......

IF (interceptors! = null) {

RegisterComponentInterceptor (Container, Clazz, Interceptors);

}

.......

Private Void RegisterComponentInterceptor (NanoContainer Container,

Class Component, String Pointcuts, String InterceptorKey) {

IF (notset) {

Pointcuts = all_methods;

}

IF (Container.getPico () InstanceOf aspectablepicocontainer {

AskECTABLEPICOCONTAINER AspectContainer = (AspectablePicocontainer) Container

. Getpico ();

AspectContainer.RegisterInterceptor (Cuts.instanceSof (Component),

GetMethodPointcut (Pointcuts), New InterceptorDelegate

aspectContainer, interceptorKey));

}

}

.......

Private Map CreateInterceptors (Element Element) {

Map interceptormap = new hashmap ();

Nodelist children = element.getchildnodes ();

For (int i = 0; i

IF (Children.Item (i) InstanceOf Element {

Element childelement = (element) Children.Item (i);

IF (aspect.equals (childelement.getnodeename ())) {

String interceptor = childelement.getattribute (interceptor);

IF (instet))

Throw new nanocontainermarkupexception ("'" key

"'Attribute Not Specified for"

Element.getnodename ());

String pointcuts = childelement.getattribute (PointCuts);

InterceptorMap.Put (Interceptor, Pointcuts);

}

}

}

IF (InterceptorMap.Isempty ())

Return NULL;

Else

Return Interceptormap;

}

Private methodPointcut getMethodPointCut (String pointcuts) {

IF (Object_Methods.equals (Pointcuts))

Return Cuts.ObjectMethods ();

IF (is_methods.equals (pointcuts))

Return cuts.ismthods ();

IF (all_methods.equals (pointcuts))

Return Cuts.allmeth 4;

IF (GET_METHODS.EQUALS (PointCuts))

Return Cuts.getMethods ();

IF (set_methods.equals (pointcuts))

Return Cuts.SetMethods (); Return Cuts.Signature (Pointcuts);

}

ContainerRecorder => aspectableContainerRecorder.java

Public MutablePicocontainer getContainerProxy () {

IF (this.Container InstanceOf aspectablepicocontainer)

Return (MutablePicoContainer) proxy.newproxyinstance (getclassloader (),

New class [] {aspectablepicocontainer.class}, invocationRecorder);

Else

Return (MutablePicoContainer) proxy.newproxyinstance (getclassloader (),

New class [] {mutablepicocontainer.class}, invocationRecorder);

}

DefaultlifecycleContainerbuider => aspectablelifecyclecontainerbuider.java

Protected PicoContainer CreateContainer (PicoContainer ParentContainer, Object AssemblyScope) {

AspectablePicocontainerFactory ContamericFactory = New DynaopaspectablePicOContainerFactory ();

Return ContainerFactory.createContainer (PARENTCONTAINER);

}

ServletContainerListener => AspectAbleServletContainerListener.java

Private Container CreateBuilder (ServletContext Context)

Throws classnotfoundexception {

......

IF (initParameter.equals) {

ContainerComposer ContainerComposer =

CreateContainerComposer (CONTEXT);

Return New AspectAblelifeCyCleContainerBuilder (ContainerComposer);

}

InterceptOrdElegate .java

Import Org.AopAlliance.Intercept.MethodInterceptor;

Import Org.AopAlliance.Intercept.MethodInvocation;

Import org.picocontainer.picocontainer;

Import dynaop.interceptor;

Import dynaop.invocation;

Public class interceptordelegate implements interceptor, methodinterceptor {

Private picocontainer pico;

Private Object Key;

Public InterceptorDelegate (PicoContainer Pico, Object Key) {

THIS.PICO = Pico;

THIS.KEY = KEY;

}

/ * (non-javadoc)

* @see dynaop.interceptor # intercept (Dynaop.Invocation) * /

Public Object Intercept (Invocation Invocation) throws throwable {

Return (Interceptor) Pico.getComponentInstance (key). Intercept (invocation);

}

/ * (non-javadoc)

* @see org.aopalliance.intercept.methodinterceptor # invoke (org.aopalliance.intercept.MethodInvocation)

* /

Public Object Invoke (MethodInvocation Invocation) throws throwable {

Return (MethodInterceptor) Pico.getComponentInstance (key)). Invoke (Invocation);

}

}

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

New Post(0)