silverb77
silverb77

Reputation: 279

Spring caching issues when using AspectJ LTW

I'm using cache abstraction mechanism from Spring 3.0 RC1 : I've set up bytecode (based on AspectJ) weawing so that cache mechanism can be applied to methods called from within the class itself. It is worth saying that first I was using proxy-based approach : everything was working fine (for methods are invoked from another object.)

As soon as I switch to AspectJ (I activated LTW via , added the right jars into their place - everything is working fine, no exceptions are thrown),no caching takes place

Any suggestion? Thank you.

==== later edit ========

I set up the logs to DEBUG for org.springframework.

If using proxy mode, I can clearly see the message Adding cacheable method 'getLargeAssetContent'.... where getLargeAssetContent is my "cacheable" method...(please see bellow)

If using aspectj mode, I don't see this message....every requests goes to the DAO layer...where as, in situation where cache works, requests stop at service layer.

What I am doing wrong? Do I need an aop.xml? I was not using AOP...., so I don't have yet an aop.xml.

Thank you for your help.

*> *2011-12-12 16:38:55,998 DEBUG [org.springframework.cache.annotation.AnnotationCacheOperationSource]

(http-127.0.0.1-8080-6) Adding cacheable method 'getLargeAssetContent' with attribute: [CacheOperation[public com.mycompany.myprj.model.AssetContent com.mycompany.myprj.dao.jcr.AssetDAOImpl.getLargeAssetContent(java.lang.String) throws com.mycompany.myprj.dao.MyPrjPersistenceException] caches=[assets] | condition='' | key='#nodeId'] 2011-12-12 16:38:56,013 INFO [com.mycompany.myprj.dao.jcr.AssetDAOImpl] (http-127.0.0.1-8080-6) Getting the content (getLargeAssetContent) of asset from node with id=575d8dc0-01be-41e4-85ce-a654fab97fe8 2011-12-12 16:38:56,092 INFO [com.mycompany.myprj.dao.jcr.AssetDAOImpl] (http-127.0.0.1-8080-6) Returning the content of asset from node with id=575d8dc0-01be-41e4-85ce-a654fab97fe8**

*

//the content is cached by now 2011-12-12 16:38:57,654 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (http-127.0.0.1-8080-6) Returning cached instance of singleton bean 'assetController' 2011-12-12 16:38:57,654 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-127.0.0.1-8080-6) Last-Modified value for [/myprj/asset/get/575d8dc0-01be-41e4-85ce-a654fab97fe8] is: -1 2011-12-12 16:38:57,654 INFO [com.mycompany.myprj.services.AssetService] (http-127.0.0.1-8080-6) Getting asset with id: 57

*

Upvotes: 5

Views: 2709

Answers (2)

Kilokahn
Kilokahn

Reputation: 2311

I tried it with a sample project and needed to do the following to switch from regular proxy to aspectj LTW

  • Change <cache:annotation-driven mode="aspectj"/>
  • Add aspectj-weaver and spring-aspects to the classpath (easy to identify in face of the ClassNotFoundException thrown)
  • Have a default aop.xml in a META-INF/aop.xml (this one is with default settings and configuration to proxy classes in my specific package - otherwise it will proxy classes in the entire JVM - which you need to take a call on)
  • Add <context:load-time-weaver aspectj-weaving="on"/> (Default is autodetect which will silently fail if META-INF/aop.xml is not found)
  • Add the javaagent switch the VM and point it to the spring-instrument jar if you are using it standalone. If you are in a container you can either use the javaagent or have it configured to a specific classloader as mentioned in the reference.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

Make sure AspectJ mode is enabled (see 28.3.3 Enable caching annotations):

<cache:annotation-driven mode="aspectj"/>

By default caching abstraction uses proxy mode, despite having LTW enabled (should switch automatically to aspectj IMHO, but it doesn't).

If this does not help examine stack trace when calling cached method from outside and from inside - what are the differences?

Upvotes: 1

Related Questions