2

出于某种原因,我的 Spring AOP 建议被调用了两次。我检查了:

  1. Spring AOP 建议被调用了两次,但我没有使用 Component 注释,而是声明了一次切面 bean 并使用 @Aspect 对其进行了注释,仅此而已。

  2. 有点晚了,我意识到我的一个类还没有实现接口,这导致了 CGLIB2 需求。我解决了这个问题,CGLIB2 问题消失了,但双重调用仍然存在。

编辑:

忘了说我查过了,建议的方法没有被调用两次。

第二次编辑:

我用@Aspect 声明一个类,然后在应用程序上下文中将它声明为一个bean。XML 文件中没有建议或切入点。

第三次编辑:

另外值得注意的是,我在执行周围建议的方法之前和之后记录:

log.info("before");

pjp.proceed();

log.info("after");

我看到的是:

before
before
after
after

这真的很奇怪。

我设置的@Before 和@Around 建议都会发生这种情况。我还没有尝试过其他类型。

这是切入点声明,但名称已更改:

@Around("execution(public java.util.List<java.lang.String> pac.age.names.myDAO.doSomething(java.lang.String, java.lang.String))")

有任何想法吗?

谢谢,

浮潜

4

3 回答 3

3

好吧,看来这实际上是记录器的问题。今天早上我检查并发现所有内容都被记录了两次。当我用常规的 sysout 调用替换记录器调用时,一切正常。

于 2012-07-17T14:25:08.607 回答
1

I had the same problem. My double aspect call was caused by me adding a parent to the aspect and also having an @Before annotation on a method in the parent that the child called in its own @Before. The Parent @Before was being inherited as a aop advice which also matched my pointcut. Therefore in addition to me explicitly calling from the child i.e(super.someMethod()), I also implicitly was calling it right after the child's advice finished executing. Removing parents @Before on the inherited 'someMethod()' fixed the aspect being called twice. So you can inherit advice. Long story short.

于 2019-08-01T23:01:53.790 回答
0

The problem with the duplicate logs comes from the "additivity" property of the Logger implementation. You can configure your logger with additivity=false (either in XML or .property file).

log4j2.logger.youraspect-logging.name=your.package.YourAspect
log4j2.logger.youraspect-logging.level=debug
log4j2.logger.youraspect-logging.appenderRef.stdout.ref=STDOUT
log4j2.logger.youraspect-logging.additivity=false
于 2021-01-26T10:06:16.820 回答