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

查看:31
本文介绍了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天全站免登陆