Spring AOP:在运行的应用程序中添加建议 [英] Spring AOP: Adding advice in a running application

查看:0
本文介绍了Spring AOP:在运行的应用程序中添加建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不重新启动服务器的情况下添加或删除正在运行的应用程序中的Spring AOP代理?

类似这样的

    GenericApplicationContext ctx = new GenericApplicationContext();
    BeanDefinitionBuilder promotion4Advice = BeanDefinitionBuilder.rootBeanDefinition(Promotion4Action.class).addPropertyValue("discountPercentage", 0.5);
    promotion4Advice.addPropertyValue("discountCode", 16);
    promotion4Advice.addPropertyValue("discountComment", "50% on regular item");
    ctx.registerBeanDefinition("promotion4Advice", promotion4Advice.getBeanDefinition());

    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ProxyFactoryBean.class);
    builder.addPropertyValue("proxyTargetClass", true);
    builder.addPropertyValue("interceptorNames", new String[] {"promotion4Advice"});
    ctx.registerBeanDefinition("proxyFactoryBean", builder.getBeanDefinition());

我的XML配置如下所示:

<bean id="promotion4Advice"
    class="com.promotion.actions.Promotion4Action">
    <property name="discountPercentage" value="0.5" />
    <property name="discountCode" value="16" />
    <property name="discountComment" value="50% on regular item" />
</bean>
<aop:config proxy-target-class="true">
    <aop:aspect id="promotion4Aspect" ref="promotion4Advice">
        <aop:pointcut id="promotion4PointCut"
            expression="execution(* com.controller.ShoppingBagController.defaultHandler(javax.servlet.http.HttpServletRequest)) and args(request)" />

        <aop:before pointcut-ref="promotion4PointCut" method="applyPromotion4"
            arg-names="request" />
    </aop:aspect>
    <aop:aspect id="promotion4Aspect1" ref="promotion4Advice">
        <aop:pointcut id="promotion4PointCut1"
            expression="execution(* com.controller.ReviewOrderController.handleRequest(javax.servlet.http.HttpServletRequest)) and args(request)" />

        <aop:before pointcut-ref="promotion4PointCut1" method="interceptOrderDetails"
            arg-names="request" />
    </aop:aspect>
    <aop:aspect id="promotion4Aspect4" ref="promotion4Advice">
        <aop:pointcut id="promotion4PointCut4"
            expression="execution(* com.controller.ShoppingBagController.applyPromoCode(javax.servlet.http.HttpServletRequest, String, String)) and args(request, promoCode, mappedURL)" />

        <aop:after pointcut-ref="promotion4PointCut4" method="interceptPromoCode"
            arg-names="request,promoCode,mappedURL" />
    </aop:aspect>
</aop:config>

这是促销活动之一……和上面一样,我还有另外3个,我希望能够通过AOP动态配置它们,而不需要更改XML和重新启动服务器。请帮帮忙

推荐答案

我认为您不能,主要是因为Spring在上下文启动期间连接了Bean。这意味着如果BeanA被注入到BeanB中,并且前一个Bean不是带有任何代理的包装器,那么它将直接注入。

当然,现在您可以获取A,将其包装在代理中,然后将其放回容器中(作为副本A')。但是B根本不知道A'

如果您事先知道哪些Bean受到动态添加/删除方面的影响,请急切地将它们包装在启动时不做任何事情的方面中(例如,调用Sort ofNoOpStrategy)。当您需要"添加"方面时,只需将该策略更改为其他方面。

这篇关于Spring AOP:在运行的应用程序中添加建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆