qianjava / ehcache-spring-annotations

Automatically exported from code.google.com/p/ehcache-spring-annotations
0 stars 0 forks source link

Possible issue when calling an annotated method within the same class #63

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
First, I'm not totally sure this is an issue, but have spent a few hours 
looking at it and believe I have it narrowed down enough to talk intelligently 
about it.

I can provide more detailed information from my side if needed.  Trying to keep 
it somewhat small for now in case a solution is immediately obvious.

What steps will reproduce the problem?

1. Set up a spring app to use ehcache and the ehcache-spring-annotations.  
Configure the com.googlecode.ehcache logger at TRACE.

2. Create a class with two getter methods, similar to the following:

    public Institution getInstitution(String id) {
        return getInstitutionList(Collections.singletonList(id)).get(0);
    }

    @Cacheable(cacheName = "InstitutionList", selfPopulating = true)
    public List<Institution> getInstitutionList(List<String> ids) {
        // logic goes here to look up the list
        return the list;
    }

3. Create another class "Caller" that calls getInstitution and run it.  In this 
case, the EhCacheInterceptor doesn't trigger.

4. Change "Caller" to call getInstitutionList instead and run it.  This time, 
the EhCacheInterceptor does trigger.

What is the expected output? What do you see instead?

I expected EhCacheInterceptor to trigger in both cases, since in the first 
case, the call is chained to the method that is annotated.

What version of the product are you using? On what operating system?

Windows is the os.  Here's the version I'm using

<dependency>
  <groupId>com.googlecode.ehcache-spring-annotations</groupId>
  <artifactId>ehcache-spring-annotations</artifactId>
  <version>1.1.2</version>
</dependency>

Please provide any additional information below.

I turned on trace logging for com.googlecode.ehcache to determine that the 
EhCacheInterceptor wasn't firing under the conditions I listed above.  Even 
when it wasn't firing, it looked like it was recognized, as I'd always see a 
line as the app came up like the following: Adding CACHE advised method....

It seems like anytime I call an @Cacheable annotated method from a different 
class, the EhCacheInterceptor fires, but if I call an annotated method from 
within a class, it doesn't.

I can provide more information if needed.  Thanks for your help and for this 
annotations package.  I've been really happy with it.

Original issue reported on code.google.com by mikeboy...@gmail.com on 25 Jan 2011 at 8:58

GoogleCodeExporter commented 8 years ago
We currently only support proxy based aspects, these work by replacing the 
spring bean you register in your XML context with a proxy that handles the 
aspect then delegates to your actual bean. The big problem with this approach 
is that self-invocation does not go through the proxy.

So in you're example Spring creates a "ServiceProxy" wrapper for your Service 
bean that intercepts calls to getInstitutionList and delegates to our 
interceptor. When Spring is done wiring you app you have:

Caller -> ServiceProxy -> Service even though what it looks like you should 
have by just reading your context config is Caller -> Service.

When Service calls one of its own methods it is just using the "this" reference 
and has no idea that ServiceProxy exists or that calls coming from other 
classes are using it.

The solution to this problem is for ehcache-spring-annotations to add AspectJ 
weaving support. AspectJ weaving allows for either compile or load-time byte 
code modification of .class files to add the interceptor call into the 
annotated class instead of having a proxy layer. Unfortunately Nick and I 
haven't ever played with AspectJ weaving before and currently don't have time 
to really look into it. It is a feature we would like to have. I created Issue 
64 as a general tracker for this functionality.

Original comment by eric.dalquist on 25 Jan 2011 at 10:17

GoogleCodeExporter commented 8 years ago
Understood.  Thanks for the detailed explanation.

Original comment by mikeboy...@gmail.com on 27 Jan 2011 at 2:47

GoogleCodeExporter commented 8 years ago
Would be nice to mention it in the documentation. I spent quite some time today 
trying to figure out why a half of my caches is not working.

Original comment by dzmitry....@gmail.com on 4 Apr 2011 at 5:20