title: Using Spring cache annotation in multiple modules
body: <p>I have a util module that produces a jar to be used in other applications.  I'd like this module to use caching and would prefer to use Spring's <code>annotation-driven</code> caching.</p>
  
  <p>So <code>Util-Module</code> would have something like this:</p>
  
  <hr>
  
  <p><strong>DataManager.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>...
  @Cacheable(cacheName="getDataCache")
  public DataObject getData(String key) { ... }
  ...
  </code></pre>
  
  <p><strong>data-manager-ehcache.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;cache name="getDataCache" maxElementsInMemory="100" eternal="true" /&gt;
  ...
  </code></pre>
  
  <p><strong>data-manager-spring-config.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;cache:annotation-driven cache-manager="data-manager-cacheManager" /&gt;
  &lt;!-- ???? ---&gt;
  &lt;bean id="data-manager-cacheManager" 
      class="org.springframework.cache.ehcache.EhcacheCacheManager" 
      p:cache-manager="data-manager-ehcache"/&gt;
  &lt;bean id="data-manager-ehcache" 
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
      p:config-location="data-manager-ehcache.xml"/&gt;
  ...
  </code></pre>
  
  <hr>
  
  <p>I would also like my deployable unit to have caching via Spring annotation, while including the above jar as a dependency. So my <code>Deployable-Unit</code> would have something like this:</p>
  
  <hr>
  
  <p><strong>MyApp.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>...
  @Cacheable(cacheName="getMyAppObjectCache")
  public MyAppObject getMyAppObject(String key) { ... }
  ...
  </code></pre>
  
  <p><strong>my-app-ehcache.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;cache name="getMyAppObjectCache" maxElementsInMemory="100" eternal="true" /&gt;
  ...
  </code></pre>
  
  <p><strong>my-app-spring-config.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;cache:annotation-driven cache-manager="my-app-cacheManager" /&gt;
  &lt;!-- ???? ---&gt;
  &lt;bean id="my-app-cacheManager" 
      class="org.springframework.cache.ehcache.EhcacheCacheManager" 
      p:cache-manager="my-app-ehcache"/&gt;
  &lt;bean id="my-app-ehcache" 
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
      p:config-location="my-app-ehcache.xml"/&gt;
  ...
  </code></pre>
  
  <hr>
  
  <h2>Question:</h2>
  
  <p>Is it possible to use annotation driven caching in both your main project and a dependency module while keeping the configurations separated?</p>
  
  <p>If not, an explanation of why it isn't would be appreciated.  If so, an explanation of what needs to change in the above configuration would be appreciated.  </p>