Pointcut for methods with @Scheduled Spring annotation

I want to have a pointcut for AspectJ for methods annotated with @Scheduled . I 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") } 

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

+4
source share
1 answer

The pointcut pointer you are looking for can be listed 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'm not sure if all you need or not. Therefore, I will also post other parts of the solution.

First of all, pay attention to the @Aspect annotation in the class. The methods of this class are required to be used as advice .

In addition, you must ensure that the class that the @Scheduled method @Scheduled is detected by scanning. You can do this by annotating this class with @Component annotation. For example:

 @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 the following:

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

Source: https://habr.com/ru/post/1483968/


All Articles