java - Spring and Ehache : Cachemanger -


i trying use eh-cache 2.8 spring 4.2.3, found can use spring ehcache manager in blog found can directly use ehcache manager directly in spring , use ehache annotataion . confused better , right approach. below code configuration :

applicationcontext.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"      xmlns:p="http://www.springframework.org/schema/p"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:cache='http://www.springframework.org/schema/cache'      xmlns:jee="http://www.springframework.org/schema/jee"     xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:jms="http://www.springframework.org/schema/jms"     xsi:schemalocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee.xsd  http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx.xsd  http://www.springframework.org/schema/jms       http://www.springframework.org/schema/jms/spring-jms.xsd ">      <context:annotation-config />     <cache:annotation-driven />     <tx:annotation-driven proxy-target-class="true" />     <tx:jta-transaction-manager />      <bean id='ehcache' class='org.springframework.cache.ehcache.ehcachemanagerfactorybean'         p:configlocation='classpath:ehcache.xml' p:shared='true' />      <bean id='cachemanager' class='org.springframework.cache.ehcache.ehcachecachemanager'         p:cachemanager-ref='ehcache' />  </beans> 

code i'm adding data cache

subscribedata.java class gets data mq , adds cache

import javax.jms.message; import javax.jms.messagelistener; import javax.jms.textmessage;  import org.springframework.beans.factory.annotation.autowired; import org.springframework.cache.cache; import org.springframework.cache.cachemanager;  public class subscribedata implements messagelistener {      @autowired     cachemanager chachemanager;      public void onmessage(message m) {         textmessage message = (textmessage) m;         try {             cache cache = chachemanager.getcache("cache");             cache.put("key", message.gettext());         } catch (exception e) {             e.printstacktrace();         }     }  } 

in subscribedata.class i'm getting data xml , adding cache , right way it. know i'm not using cache annotation here , there other methods i'm using it.

below queries

1) mentioned above should use spring ehcache or ehcache directly example below ehcache cache manager instead of spring ehcache manager

@configuration @enablecaching(proxytargetclass = true) public class cacheconfig {     @bean     public cachemanager cachemanager() {         net.sf.ehcache.cachemanager ehcachecachemanager = ehcachemanagerfactorybean().getobject();         return new ehcachecachemanager(ehcachecachemanager);     }      @bean     public ehcachemanagerfactorybean ehcachemanagerfactorybean() {         ehcachemanagerfactorybean cachemgrfb = new ehcachemanagerfactorybean();          cachemgrfb.setshared(true);         cachemgrfb.setcachemanagername("cachemanager");         cachemgrfb.setconfiglocation(ehcacheconfigresource);         return cachemgrfb;     } } 

2) how add data cache in void method i.e methods dont return , want add data cache.

  1. for first question, not using ehcache api in second example, writing code equivalent spring xml above. in context, unless have reason have code this, stick xml make updating configuration easier if needs 1 day.
  2. you provide answer in subscribedata sample. cannot use void method proxy doing wrapping not have access data needs published.

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -