方法注释的方面没有被执行

时间:2014-01-22 10:45:41

标签: java spring annotations aop spring-aop

我正在尝试创建一个方面来监视某些方法的执行时间。我将注释定义为:

@Retention(RetentionPolicy.RUNTIME)
@Target(
{
    ElementType.METHOD, 
    ElementType.TYPE
})
public @interface TimePerformance {

}

这是方面代码:

@Around("execution(* *(..)) && @annotation(timePerformance)")
    public Object  timePerformance(ProceedingJoinPoint pjp,TimePerformance timePerformance) throws Throwable {

        if (LOG.isInfoEnabled()) {
             LOG.info("AOP - Before executing "+pjp.getSignature());
        }

        Long startTime = System.currentTimeMillis();

        Object result = pjp.proceed();

        Long stopTime = System.currentTimeMillis();

        LOG.info("MONITOR TIME_EXECUTION "+pjp.getSignature()+" : "+(stopTime-startTime));

        if (LOG.isInfoEnabled()) {
             LOG.info("AOP - After  executing "+pjp.getSignature());
        }

        return result;

    }

配置为:

<!-- AOP support -->
<bean id='stateAspectImpl' class='eu.genetwister.snpaware.ui.aspect.StateAspectImpl' />
 <bean id='monitorImpl' class='eu.genetwister.snpaware.monitor.MonitorImpl' />
<aop:aspectj-autoproxy>
    <aop:include name='stateAspectImpl' />
     <aop:include name='monitorImpl' />
</aop:aspectj-autoproxy>

我已经注释了一个方法(弹簧批处理作业的一部分),如下所示:

@BeforeStep
    @TimePerformance
    public void retrieveInterstepData(StepExecution stepExecution)

但即使执行该方法,该方面也永远不会被执行。

有没有人知道如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:1)

为什么在切入点表达式中需要execution(..)? 我相信@Around(value = @annotation(<full name of the class including the package>)")应该有效。 因为,您使用注释说,无论使用此注释注释哪个方法都需要进行检查并调用@Around建议。

在我看来你不需要执行表达式,因为即使你的自定义注释可以应用于字段,Spring AOP也不支持字段上的Aspects。

另外,你需要绑定方面的对象吗?您也可以从pjp.getArgs();获取对象以防万一。

编辑: 这是方面

@Component(value = "regionAccessValidatorAspect")
@Aspect
public class RegionAccessValidatorAspect {

   @Around(value = "@annotation(com.....RegionAccessValidator)")
   public Object doAccessCheck(final ProceedingJoinPoint jp) throws Throwable {

..... }

这是我设置注释的处理器

   @RegionAccessValidator(getVal = "Temporary")
   public CountryProductOfferingRepresentation update(final CountryProductOfferingRepresentation cpoRep,
         final RequestType requestType) throws Exception {

这是注释

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RegionAccessValidator {

   String getVal();

}

虽然,我还没有试过通过一个论点,我今天会尝试,并找出是否是原因。

相关问题