程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 在nanowar中引入AOP

在nanowar中引入AOP

編輯:關於JAVA
通過nanocontainer的nanowar和其struts插件完美的實現了IoC模式的透明,但無法在標准的nanowar中引入nanoaop不得不說是一種遺憾。看了一下nanocontainer的代碼,決定自己動手給nanowar添加aop的支持。要在組件和實例中支持AOP,首先要引入interceptor chains。可以考慮在XML定義中引入標簽。這個標簽與組件或實例的標簽在同一級別上。其中interceptor指出攔截器的名稱。攔截器的實現可以通過標准組件方式在同一個配置文件中進行注冊。pointcuts是攔截方法的過濾表達式(perl5的正則表達式,和nanoaop預定義幾個過濾規則)。在nanowar中實現這個標簽,需要修改ScopedCotainerComposer, XMLContainerBuilder, ContainerRecorder, ServletContainerListener, DefaultLifecycleContainerBuider類。由於攔截器無法在IoC容器初始化的過程中得到實例。因此,還需要引入一個新的通用攔截器類(InterceptorDelegate),作為真正的攔截器實例和需被攔截的組件的調用橋梁。由於對源代碼改動量較大,索性就在原來類的前面加上Aspectable前綴引入新的類,但除了AOP部分,其他的保持不變。ScopedCotainerComposer => AspectableScopedContainerComposer.java public AspectableScopedContainerComposer() throws ClassNotFoundException { this(new DynaopAspectablePicoContainerFactory().createContainer()); }XMLContainerBuilder => AspectableXMLContainerBuilder.javaprotected PicoContainer createContainerFromScript( .... AspectablePicoContainerFactory containerFactory = 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)) { pointcuts = ALL_METHODS; } if (container.getPico() instanceof AspectablePicoContainer) { AspectablePicoContainer 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 < children.getLength(); i++) { if (children.item(i) instanceof Element) { Element childElement = (Element) children.item(i); if (ASPECT.equals(childElement.getNodeName())) { String interceptor = childElement.getAttribute(INTERCEPTOR); if (notSet(interceptor)) 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.isMethods(); if (ALL_METHODS.equals(pointcuts)) return cuts.allMethods(); 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(getClass().getClassLoader(), new Class[]{AspectablePicoContainer.class}, invocationRecorder); else return (MutablePicoContainer) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{MutablePicoContainer.class}, invocationRecorder); }DefaultLifecycleContainerBuider => AspectableLifecycleContainerBuider.java protected PicoContainer createContainer(PicoContainer parentContainer, Object assemblyScope) { AspectablePicoContainerFactory containerFactory = new DynaopAspectablePicoContainerFactory(); return containerFactory.createContainer(parentContainer); }ServletContainerListener => AspectableServletContainerListener.javaprivate ContainerBuilder createBuilder(ServletContext context) throws ClassNotFoundException { ...... if (initParameter.equals(CONTAINER_COMPOSER)) { ContainerComposer containerComposer = createContainerComposer(context); return new AspectableLifecycleContainerBuilder(containerComposer); }InterceptorDelegate .javaimport 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); }}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved