具有@Scheduled Spring批注的方法的切入点 [英] Pointcut for methods with @Scheduled Spring annotation

查看:100
本文介绍了具有@Scheduled Spring批注的方法的切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用@Scheduled注释的方法设置AspectJ切入点.尝试了不同的方法,但无济于事.

I want to have a AspectJ pointcut for methods annotated with @Scheduled. Tried different approaches but nothing worked.

1.)

@Pointcut("execution(@org.springframework.scheduling.annotation.Scheduled * * (..))")
public void scheduledJobs() {}

@Around("scheduledJobs()")
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable {
    LOG.info("testing")
}

2.)

@Pointcut("within(@org.springframework.scheduling.annotation.Scheduled *)")
public void scheduledJobs() {}

@Pointcut("execution(public * *(..))")
public void publicMethod() {}

@Around("scheduledJobs() && publicMethod()")
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable {
    LOG.info("testing")
}

有人可以建议其他方法对@Scheduled带注释的方法提出around/before建议吗?

Can anyone suggest any other way to have around/before advice on @Scheduled annotated methods?

推荐答案

您要查找的切入点可以指定如下:

The pointcut that you are looking for can be specified as below:

@Aspect
public class SomeClass {

    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void doIt(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("before");
        pjp.proceed();
        System.out.println("After");
    }
}

我不确定这是否就是您所需要的.因此,我还将发布解决方案的其他部分.

I am not sure whether that's all you require or not. So I'm going to post the other parts of the solution as well.

首先,请注意该类上的@Aspect批注.此类中的方法必须用作advice.

First of all, notice the @Aspect annotation on the class. It is required for the methods in this class to be applied as advice.

此外,您需要确保通过扫描可检测到具有@Scheduled方法的类.您可以通过使用@Component批注对该类进行批注来实现.例如:

Also, you need to make sure that the class that has the @Scheduled method is detectable via scanning. You can do so by annotation that class with @Component annotation. For ex:

@Component
public class OtherClass {
    @Scheduled(fixedDelay = 5000)
    public void doSomething() {
        System.out.println("Scheduled Execution");
    }
}

现在,要执行此操作,弹簧配置中所需的零件如下:

Now, for this to work, the required parts in your spring configuration would be as follows:

<context:component-scan base-package="com.example.mvc" />
<aop:aspectj-autoproxy />   <!-- For @Aspect to work -->    
<task:annotation-driven />  <!-- For @Scheduled to work -->

这篇关于具有@Scheduled Spring批注的方法的切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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