0

I am trying to intercept calls to the find method in EntityManager.

public Map<String, String> get() {
        Map<String, String> map = new HashMap<>();
        DleTestData data = em.find(DleTestData.class, "1");
        map.put(data.getId(), data.getName() + " : " + data.getRegion());
        return map;
    }

I have an advice written like this:

@Aspect
@Configuration
public class MyAdvice {

    @Around("execution(* javax.persistence.EntityManager.*(..))")
    public Object aroundFind(ProceedingJoinPoint joinPoint) {
        System.err.println("before em find called : " + joinPoint);
        Object o = null;
        try {

            o = joinPoint.proceed();

            System.err.println("after em find advice called : " + joinPoint);

        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return o;

    }

}

The output show calls intercepted but the find method doesn't get matched in the pointcut. Can you suggest what am I doing wrong here?

output:

before em find called : execution(Metamodel javax.persistence.EntityManager.getMetamodel()) after em find advice called : execution(Metamodel javax.persistence.EntityManager.getMetamodel())

RCInd
  • 344
  • 2
  • 13

1 Answers1

2

The Spring AOP manual states that Spring AOP only works for Spring beans/components.

The same manual also describes how you can apply AOP to non-Spring classes via full AspectJ via LTW (load-time weaving). It is pretty easy to configure.

If you experience any problems weaving into a basic class from the javax..* package because maybe the class is loaded before LTW is activated (even though you should be able to do that if you use javaagent:/path/to/aspectjweaver.jar), you can still switch from execution() to call() pointcut. As long as the calls are in your own application code it should be easy to intercept via AspectJ. But you do need AspectJ for it, not Spring AOP, because the latter neither supports non-Spring beans (as mentioned above) nor call() pointcut (as mentioned in the Spring manual).


Update after OP's comment:

I just checked the EntityManager Javadoc for you: Method getMetaModel() is part of the interface while get() is not. Consequently, the pointcut fails to find it.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • By that logic...the question is that the calls to getMetaModel() which is defined in the same interference as find() and also has the same implementation class as find gets intercepted but the same doesn't work for find(). Also when u say LTW, you mean using @Configurable ? Of course xml driven here as the class.is a framework class. Isnthere a way I could do this easily with SpringBoot? – RCInd May 06 '18 at 06:53
  • Thanks you for the efforts kriegaex. however, i just checked and foundteh find() method very much in the same javadocs link as you shared, T find(Class entityClass, Object primaryKey) Find by primary key. – RCInd May 07 '18 at 05:29
  • Oh sorry, I thought you wanted to intercept the `get()` method you showed in your question. I overread that `find(..)` is your problematic case. Anyway, I cannot see which class `get(..)` belongs to and if that class is a Spring bean. Would you please edit your question and provide a full [MCVE](http://stackoverflow.com/help/mcve) so I can stop guessing and start reproducing and analysing your problem? Extract a minimal example, buildable and compilable, optimally with Maven POM and cloneable from GitHub. You could have so many problems, I am getting tired of guessing just now. – kriegaex May 07 '18 at 13:20
  • I found another question here and I don't even see an answer for this so it seems this has more to do than mistakes in my configuration.:https://stackoverflow.com/questions/32934397/configuring-hibernate-session-with-spring-aop – RCInd May 10 '18 at 09:08
  • i will extract and upload a minimal example, but for now I am using a workaround from the link above. – RCInd May 10 '18 at 09:14