简体   繁体   English

按方法注释未执行

[英]Aspect by method annotation not being executed

I am trying to create an aspect to monitor the time execution of certain methods. 我正在尝试创建一个方面来监视某些方法的时间执行。 I define the annotation as: 我将注释定义为:

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

}

And this is the aspect code: 这是方面代码:

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

    }

And the configuration is: 且配置为:

<!-- 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>

I have been annotated a method (part of a spring batch job) like this: 我已经被注释为这样的方法(春季批处理工作的一部分):

@BeforeStep
    @TimePerformance
    public void retrieveInterstepData(StepExecution stepExecution)

But the aspect is never being executed, even if the method is executed. 但是,即使执行了该方法,也永远不会执行该方面。

Does anyone an idea about how to solve this? 有谁知道如何解决这个问题?

Thanks 谢谢

Why do you need the execution(..) in the pointcut expression? 为什么在切入点表达式中需要execution(..) I believe @Around(value = @annotation(<full name of the class including the package>)") should work. Because, you are saying by using the annotation,that, whichever method is annotated with this annotation needs to be inspected and the @Around advice invoked. 我相信@Around(value = @annotation(<full name of the class including the package>)")应该可以工作。因为,您说的是通过使用注释,即需要检查使用该注释进行注释的任何方法, @Around建议被调用。

You wouldnt need the execution expression in my opinion because, even if your custom annotation can be applied to fields, Spring AOP doesnt support Aspects on fields. 我认为您不需要执行表达式,因为即使您的自定义注释可以应用于字段,Spring AOP也不支持字段方面。

Also, do you need to bind the object in the aspect? 另外,您是否需要在方面中绑定对象? You can also get the object from pjp.getArgs(); 您还可以从pjp.getArgs();获取对象pjp.getArgs(); just in case. 以防万一。

EDIT : Here is the Aspect 编辑:这是方面

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

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

..... } .....}

Here is the processor on which i have setup my annotation 这是我在上面设置注释的处理器

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

Here is the annotation 这是注释

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

   String getVal();

}

Although, i havent tried by passing it an argument, i will try that today and find out if that is the cause. 虽然,我还没有尝试通过传递一个参数,但今天我将尝试使用该参数,并找出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM