Spring AOP建议被两次调用 [英] Spring AOP advice is called twice

查看:346
本文介绍了Spring AOP建议被两次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Spring AOP建议,但无法查明为什么两次被调用:

I have the following Spring AOP advice and I can't find out why it is called twice:

@Component
@Aspect
public class LoggingAspects {

    Logger logger = LoggerFactory.getLogger(LoggingAspects.class);

    @AfterReturning(pointcut = "execution(public * com.A.B.C.service.impl.*.browse(..))",
            returning = "retVal")
    public Object onBrowse(DomainClass retVal) {
        logger.info("#######################Advice Called: +retVal);
        return null;
    }

}

配置非常简单:

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<bean id="loggingCASAspect" class="com.minervanetworks.xtv.stb.service.aspects.LoggingCASAspects"/> 

我还尝试了以下建议,但结果相同(称为两次).

I have also tried the following advice with the same result (called twice).

@AfterReturning(pointcut="@annotation(com.A.B.C.service.impl.LOG)", returning="retVal")
public Object onBrowse(JoinPoint jp, DomainClass retVal) {
    logger.info("#######################Advice called! " + jp.toLongString()
    + " Target: " + jp.getTarget()
    + " Signature: " + jp.getSignature()
    + " Kind: " + jp.getKind()
    + " This: " + jp.getThis()
    + " Source Location: " + jp.getSourceLocation());

    return null;
}

上述记录器的调试信息为:

The debug info from the above logger is:

2011-10-26 11:56:01,887 [INFO][com.A.B.C.service.aspects.LoggingAspects] #######################Advice called! execution(public abstract com.A.B.C.domain.DomainClass com.A.B.C.service.ContentManager.browse(java.lang.String,java.lang.String,java.lang.String,java.lang.Boolean)) Target: com.A.B.C.service.impl.ContentManagerImpl@62ad191 Signature: DomainClass com.A.B.C.service.ContentManager.browse(String,String,String,Boolean) Kind: method-execution This: com.A.B.C.service.impl.ContentManagerImpl@62ad191 Source Location: org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@d324de2

它以完全相同的值显示两次.

It is displayed twice with exactly the same values.

推荐答案

1.提示

您使用JavaConfig还是xml? 如果您同时使用这两种方法,则可能是Spring IoC容器对Aspect进行了两次处理.

Do you use JavaConfig or xml? If you're using both, it could be that the Aspect is being processed two times by the Spring IoC container.

2.提示

我不使用@Component注释对方面进行注释,请尝试将其删除,也许是因为Spring对此进行了两次处理.

I don't annotate aspects with @Component annoation, try to remove it, maybe because of that is being processed twice by Spring.

这篇关于Spring AOP建议被两次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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