Java Spring aop 多个切入点和建议,但只有最后一个有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2763646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring aop multiple pointcuts & advice but only the last one is working
提问by Jarle Hansen
I have created two Spring AOP pointcuts that are completely separate and will be woven in for different parts of the system. The pointcuts are used in two different around advices, these around-advices will point to the same Java method.
我创建了两个完全独立的 Spring AOP 切入点,它们将被编入系统的不同部分。切入点用于两个不同的环绕通知,这些环绕通知将指向同一个 Java 方法。
How the xml file looks:
xml 文件的外观:
<aop:config>
<aop:pointcut expression="execution(......)" id="pointcutOne" />
<aop:pointcut expression="execution(.....)" id="pointcurTwo" />
<aop:aspect id="..." ref="springBean">
<aop:around pointcut-ref="pointcutOne" method="commonMethod" />
<aop:around pointcut-ref="pointcutTwo" method="commonMethod" />
</aop:aspect>
</aop:config>
The problem is that only the last pointcut works (if I change the order pointcutOne
works because it is the last one). I have gotten it to work by creating one big pointcut, but I would like to have them separate. Any suggestions to why only one of the pointcuts works at a time?
问题是只有最后一个切入点有效(如果我更改顺序pointcutOne
有效,因为它是最后一个)。我已经通过创建一个大切入点来让它工作,但我想让它们分开。关于为什么一次只有一个切入点有效的任何建议?
采纳答案by Espen
Try to have the pointcut and advice inside the <aop:aspect>
element. Something like this:
尝试在<aop:aspect>
元素中包含切入点和建议。像这样的东西:
<aop:config>
<aop:aspect id="aspect1" ref="springBean">
<aop:pointcut expression="execution(......)" id="pointcutOne" />
<aop:around pointcut-ref="pointcutOne" method="commonMethod" />
</aop:aspect>
<aop:aspect id="aspect2" ref="springBean">
<aop:pointcut expression="execution(.....)" id="pointcurTwo" />
<aop:around pointcut-ref="pointcutTwo" method="commonMethod" />
</aop:aspect>
</aop:config>
I guess your XML configuration resulted in just one proxy object, while it should have been two proxy objects.
我猜你的 XML 配置导致只有一个代理对象,而它应该是两个代理对象。
Btw: You should consider using @AspectJ
syntax instead. It's just Java with pointcuts and advices in annotations. It works well with Spring AOP and offers more functionality than the XML alternative.
顺便说一句:您应该考虑使用@AspectJ
语法。它只是在注解中带有切入点和建议的 Java。它与 Spring AOP 配合得很好,并提供了比 XML 替代方案更多的功能。
All you need in your configuration to enable @AspectJ
aspects with Spring AOP:
在配置中启用@AspectJ
Spring AOP 方面所需的一切:
<aop:aspectj-autoproxy>
<aop:include name="aspect1" />
<aop:include name="aspect2" />
</aop:aspectj-autoproxy>
<bean id="aspect1" class="com.demo.Aspect1"/>
<bean id="aspect2" class="com.demo.Aspect2"/>
And the Aspect could be something like this:
方面可能是这样的:
@Aspect
public class Aspect1 {
@Pointcut("execution(* *(..))")
public void demoPointcut() {}
@Around("demoPointcut()")
public void demoAdvice(JoinPoint joinPoint) {}
}
Updated:
更新:
Example that uses a pointcut to combine three other pointcuts:
使用切入点组合其他三个切入点的示例:
@Pointcut("traceMethodsInDemoPackage() && notInTestClass() " +
"&& notSetMethodsInTraceDemoPackage()")
public void filteredTraceMethodsInDemoPackage() {}