Pointcut for methods with @Scheduled Spring annotation

时光总嘲笑我的痴心妄想 提交于 2019-12-04 07:24:55

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.

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

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 -->
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!