2011-10-26 78 views
4

我有以下Spring AOP的意见,我不能找出为什么它被称为两次:Spring AOP的建议被调用两次

@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"/> 

我也尝试了以下建议,结果相同(称为两次)。

@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; 
} 

从上面记录程序的调试信息是:

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: [email protected] Signature: DomainClass com.A.B.C.service.ContentManager.browse(String,String,String,Boolean) Kind: method-execution This: [email protected] Source Location: org.springframework.aop[email protected]d324de2 

它是用完全相同的值显示两次。

+0

我讨厌问明显,但被调用两次的方法? –

+0

这是第一件事,我没有检查。该方法只被调用一次。 对不起,我错过了在原来的问题中提及。 –

回答

13

1.提示

你使用JavaConfig或XML? 如果您同时使用,则可能是Spring IoC容器正在处理两次Aspect。

2.提示

我不@Component annoation注释方面,试图将其删除,可能的因为正在由Spring处理两次。

+4

这是因为@Component注释。谢谢! –

+0

提示no 2适合我 –