由网友(芭芭说名字长点才会被注意)分享简介:如何在不重新启动服务器的情况下添加或删除正在运行的应用程序中的Spring AOP代理?类似这样的GenericApplicationContext ctx = new GenericApplicationContext();BeanDefinitionBuilder promotion4Advice = BeanDe...
如何在不重新启动服务器的情况下添加或删除正在运行的应用程序中的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
)。当您需要"添加"方面时,只需将该策略更改为其他方面。
相关推荐
最新文章