Spring boot request aspect bean with @scheduled task

Viewed 33

I use in a spring boot application a component as a Spring aspect:

@RequestScope
@Component
@Aspect
public class MyComponentAspect{
     @Around("execution(public * com.mycompany..MyService.saveEntity(*))")
     public void getsaveEntityExecutionTime(ProceedingJoinPoint joinPoint) {
       ...
     }
     // other advices
}

I need to collect execution time of some methods for each http request the app receives. So as you can see, I have annotated the aspect with @RequestScope.

I Recently add a scheduled task which also needs to call the MyService.saveEntity method.

@Service
@EnableScheduling
public class MyScheduledJob {

    private MyService myService;
    // ...
    
}

Of course this is not working because the scheduledJob can't actually find the request scope bean and fails. Actually I don't want the aspect to be enabled when the scheduledJob is running.

Is there a way to let the aspect enabled for normal web request and not for disable it for scheduled task?

0 Answers
Related