views:

177

answers:

2

If I have a bunch of DAO's with a bunch of getXXX methods and I want all or some explicit list of the methods cached is there any way I can do this transparently with Spring?

What I don't want is:

  • To change any source code / add anotations
  • Manually have to create a number of proxy beans for a number of DAO's and rewire them all.

Ideally something with a regex to match the DAO's and the method's to cache and automatically wrap itself around the DAO's as needed.

We are using OSCache so an example with that would be fantastic.

A: 

Spring AOP is what you want, I think. This will give you the ability to auto-create proxy objects for your DAOs without going through them all manually.

It's a complex topic, though, so I suggest you read the relevant section of the Spring docs. As an idea to get you started, though, the BeanNameAutoProxyCreator might be useful to you. The AspextJK stuff is the full-blooded AOP approach, but it's quite scary. The Schema-based AOP approach is rather easier, but less flexible.

One of the central tenets of the Spring Framework is that of non-invasiveness; this is the idea that you should not be forced to introduce framework-specific classes and interfaces into your business/domain model. However, in some places the Spring Framework does give you the option to introduce Spring Framework-specific dependencies into your codebase: the rationale in giving you such options is because in certain scenarios it might be just plain easier to read or code some specific piece of functionality in such a way. The Spring Framework (almost) always offers you the choice though: you have the freedom to make an informed decision as to which option best suits your particular use case or scenario.

One such choice that is relevant to this chapter is that of which AOP framework (and which AOP style) to choose. You have the choice of AspectJ and/or Spring AOP, and you also have the choice of either the @AspectJ annotation-style approach or the Spring XML configuration-style approach. The fact that this chapter chooses to introduce the @AspectJ-style approach first should not be taken as an indication that the Spring team favors the @AspectJ annotation-style approach over the Spring XML configuration-style.

See the section entitled Section 6.4, “Choosing which AOP declaration style to use” for a fuller discussion of the whys and wherefores of each style.

As for the actual caching, you'll have to do that yourself, but that should be straightforward once the AOP hooks are integrated.

skaffman
A: 

You could try the caching provided by Spring Modules. It does caching via aspects and it supports several caching solutions such as OsCache, EHCache, JBoss Cache, etc.

jridley