I need to get a specific EhCache instance by name and I’d prefer to autowire if possible. Given the following automatically configured controller, how can I autowire in the cache instance I’m looking for?
@Controller
public class MyUniqueService {
...
}
<beans ...>
<ctx:component-scan base-package="my.controllers"/>
<mvc:annotation-driven />
</beans>
How do I configure EhCache in my application context? I don’t see any log messages from EhCache about it loading the ehcache.xml file in my /WEB-INF/ directory. How do I make it load it?
How can I integrate EhCache with my Spring application to have it load the ehcache.xml file from my /WEB-INF/ directory and autowire a cache by a given name into my MyUniqueService controller?
First you need to create a Ehcache CacheManager singleton in you app context like this:
Here
configLocationis set to load from classpath or usevalue="/WEB-INF/my-ehcache.xml".In your controller simply inject the
CacheManagerinstance:Alternatively, if you’d like to go the “entirely autowired” route, do:
Setup your class like so:
uniqueObjectCachecorresponds to this cache instance in yourehcache.xmlcache definition:There isn’t a way to inject an actual cache instance, but as shown above, you can inject a cache manager and use it to get the cache you’re interested in.