0

The MongoTemplate class has a nested class -QueryCursorPreparer. My task is to intercept the QueryCursorPreparer.getReadPreference () method. How could I do this? When trying to do this:

@Aspect
@Component
public class MongoInterceptor {

  @Around("execution(* org.springframework.data.mongodb.core.MongoTemplate.*(..))")
  public Object intercept(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("TEST");
    return joinPoint.proceed();
  }
}

Nothing is intercepted

  • What do you want to achieve in the end? – M. Deinum Nov 16 '20 at 09:14
  • My task is simple, I want to manage ReadPreference not from the repository level. For example, I want anatation to RestController to control what is in this thread of execution, reading will be done through Secondary members. To do this, I want to write an interceptor on MongoTemplate that would intercept the getReadPreference method and substitute the value I need there, depending on the presence of annotations in this process. – Дима Гуманов Nov 16 '20 at 09:49
  • @Dima, glancing at the [`MongoTemplate`](https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html) Javadoc it looks as if that class is not a Spring bean/component. This would explain why you cannot target it with a Spring AOP aspect. Unless you find another means to achieve your goal, you can still switch to Spring-controlled native AspectJ and get rid of that limitation. Then your aspect would work. – kriegaex Dec 05 '20 at 03:31

1 Answers1

0

Have you configured the post-compile weaving? Since you are trying to advice a 3rd party code (you don't own the source code). Maybe these could help :

Axella Geraldinc
  • 213
  • 2
  • 12