在nanowar中引入AOP

來源:互聯網
上載者:User
通過nanocontainer的nanowar和其struts外掛程式完美的實現了IoC模式的透明,但無法在標準的nanowar中引入nanoaop不得不說是一種遺憾。看了一下nanocontainer的代碼,決定自己動手給nanowar添加aop的支援。要在組件和執行個體中支援AOP,首先要引入interceptor chains。可以考慮在XML定義中引入<aspect interceptor='InterceptorName' pointcuts='Filter'/>標籤。這個<aspect>標籤與組件或執行個體的<parameter>標籤在同一層級上。其中interceptor指出攔截器的名稱。攔截器的實現可以通過標準組件方式在同一個設定檔中進行註冊。pointcuts是攔截方法的過濾運算式(perl5的Regex,和nanoaop預定義幾個過濾規則)。在nanowar中實現這個<aspect>標籤,需要修改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);    }}

相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。