1.constantdatadto.java, be sure to rewrite the hashcode and Equals methods
Import java.io.Serializable;
Import Java.util.Date; /** * * * * * * * * * @author Steveguoshao * @created July 10, 2014 6:15:55 * @version 1.0/public class Constantdatadto implements Serializable {/** * serialversionuid:todo * * @since 1.0.0/private static FINA
L Long serialversionuid = -504215100856069620l;
Private String CodeSet;
Private String Code;
Private String codecontent;
Private String Parentcode;
Private Integer Seqnum;
Private Date CJSJ;
Private Date XGSJ;
Public String Getcodeset () {return codeset;
} public void Setcodeset (String codeset) {this.codeset = CodeSet;
Public String GetCode () {return code;
public void Setcode (String code) {this.code = code;
Public String getcodecontent () {return codecontent;
} public void Setcodecontent (String codecontent) {this.codecontent = codecontent;
Public String Getparentcode () {return parentcode; } public void Setparentcode (String ParentcoDE) {this.parentcode = Parentcode;
Public Integer Getseqnum () {return seqnum;
The public void Setseqnum (Integer seqnum) {this.seqnum = Seqnum;
Public Date GETCJSJ () {return CJSJ;
The public void SETCJSJ (Date cjsj) {THIS.CJSJ = CJSJ;
Public Date GETXGSJ () {return XGSJ;
The public void Setxgsj (Date xgsj) {this.xgsj = XGSJ;
@Override public int hashcode () {final int prime = 31;
int result = 1;
result = Prime * result + ((code = = null) 0:code.hashcode ());
result = Prime * result + ((CodeSet = null)? 0:codeset.hashcode ());
return result;
@Override public boolean equals (Object obj) {if (this = = obj) {return true;
} if (obj = = null) {return false;
} if (GetClass ()!= Obj.getclass ()) {return false;
Constantdatadto other = (constantdatadto) obj;
if (Other.getcode (). Equals (code) && Other.getcodeset (). Equals (CodeSet)) {return true;
return false;
}
}
2. With Concurrenthashmap implementation of the cache Constantdatacache, just started with the concurrenthashmap when the data container, but the operation of the method is not the use of the Concurrentmap interface inside the method, Instead of using the method in the map interface, and the operation is not atomic, so can not achieve concurrency, or will appear concurrentmodificationexception, to achieve concurrency must be used Concurrentmap interface inside the method.
Concurrentmap code as follows, note the annotation of each method in the interface, which is equivalent to the original map interface inside the method implementation code
Public interface concurrentmap<k, v> extends Map<k, v> {/** * If the specified key are not already a
ssociated * With a value, associate it with the given value.
* This is equivalent to * <pre> * if (!map.containskey (key)) * Return Map.put (key, value);
* Else * return Map.get (KEY);</pre> * * */V putifabsent (K key, v value);
/** * Removes the entry for a key of only if currently mapped to a given value.
* This is equivalent to * <pre> * if (Map.containskey (key) && Map.get (key). Equals (value)) {
* Map.Remove (key);
* return true;
* Else Return false;</pre> * * Boolean Remove (object key, object value);
/** * Replaces the entry for a key of only if currently mapped to a given value.
* This is equivalent to * <pre> * if (Map.containskey (key) && Map.get (key). Equals (OldValue)) { * Map.put (key, NewValue);
* return true;
* Else return false;</pre> */boolean replace (K key, v OldValue, v newvalue);
/** * Replaces the entry for a key of only if currently mapped to some value.
* This is equivalent to * <pre> * if (Map.containskey (key)) {* Return Map.put (key, value);
* Else return null;</pre> */V Replace (K key, v value); }
Constantdatacache.java
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.concurrent.ConcurrentHashMap;
Import Java.util.concurrent.ConcurrentMap;
Import javax.annotation.PostConstruct;
Import Javax.annotation.PreDestroy;
Import Org.apache.commons.lang.StringUtils;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.context.annotation.Scope;
Import org.springframework.stereotype.Component; /** * Constantdatacache.java * @author Steveguoshao * @created July 10, 2014 2:54:46 * @version 1.0 * * @Compo nent @Scope (value= "singleton") public class Constantdatacache {@Autowired (required = true) Private Constantdataservice
Impl Constantdataservice; private static concurrentmap<string, list<constantdatadto>> CACHE = new concurrenthashmap<string, List
<ConstantDataDTO>> (); /** * Initialize cache from database * init * void * @since 1.0.0 * * * @PostConstruct private void init () {LOAD (); /** * Load Cache * Load * void * @since 1.0.0 */public void load () {set<string> codesets = Constantd
Ataservice.findallcodeset ();
for (String codeset:codesets) {cache.putifabsent (CodeSet, Constantdataservice.findbycodeset (CodeSet));
} * * Reload cache/public void reload () {cache.clear ();
Load (); /** * Get Cache * GetCache * @return * map<string,list<constantdatadto>> * @exception * @since
1.0.0 * * Public map<string, list<constantdatadto>> GetCache () {return CACHE; /** * Obtains constant data from the cache according to the code set * Getcachebycodeset * @param key * @return * list<constantdata> * @since 1
0.0 */Public list<constantdatadto> getconstantdatalist (String key) {return cache.get (key);
/** * Get constant data from cache based on code set and parent code * GETCONSTANTDATALISTBYPARENTCODE * @param key * @param parentcode * @return * list<constantdata> * @since 1.0.0/Public List<constantdatadto>
Getconstantdatalistbyparentcode (string key, String parentcode) {list<constantdatadto> dtolist = CACHE.get (key);
list<constantdatadto> resultlist = new arraylist<constantdatadto> ();
for (Constantdatadto dto:dtolist) {if (Parentcode.equals (Dto.getparentcode ())) {Resultlist.add (DTO);
} return resultlist; /** * Obtains constant data from the cache based on code set and code * GETCONSTANTDATA * @param key * @param code * @return * constantdata * @sin CE 1.0.0 */Public constantdatadto Getconstantdata (string key, string code) {if (Stringutils.isnotempty (key) &A
mp;& stringutils.isnotempty (code)) {list<constantdatadto> dtolist = Cache.get (key);
Constantdatadto dto = new Constantdatadto ();
Dto.setcodeset (key);
Dto.setcode (code);
int index = Dtolist.indexof (DTO);
if (Index >-1) {return dtolist.get (index);
} return null; /** * Get code content based on code set and code * getcodecontent * @param key * @param code * @return * StRing * @since 1.0.0 */public static string Getcodecontent (string key, String code) {String codecontent = ""; if (Stringutils.isnotempty (key) && Stringutils.isnotempty (code) {list<constantdatadto> dtolist = CA
Che.get (key);
Constantdatadto dto = new Constantdatadto ();
Dto.setcodeset (key);
Dto.setcode (code);
int index = Dtolist.indexof (DTO);
if (Index >-1) {codecontent = Dtolist.get (index). Getcodecontent ();
} return codecontent; /** * Obtains constant data from the cache based on code set and parent code * GETCACHEBYPARENTCODE * @param key * @param * @return * list<c onstantdata> * @since 1.0.0 */public static list<constantdatadto> Getcachebyparentcode (string key, String
Parentcode) {list<constantdatadto> dtolist = Cache.get (key);
list<constantdatadto> resultlist = new arraylist<constantdatadto> (); for (Constantdatadto dto:dtolist) {if (Dto.getparentcode (). Equals (Parentcode)) {RESULTLIST.ADD (DTO);
} return resultlist; /** * Adds new or updated constants to the cache * put * @param model * void * @since 1.0.0/public void putting (constantdatadto
Model) {list<constantdatadto> dtolist = Cache.get (Model.getcodeset ()); list<constantdatadto> newdtolist = Dtolist = null?
New Arraylist<constantdatadto> (): dtolist;
Newdtolist.add (model);
if (null!= dtolist && dtolist.size () > 0) {cache.replace (Model.getcodeset (), newdtolist);
else {cache.putifabsent (Model.getcodeset (), newdtolist); }/** * Remove data from cache * Remove * @param key * void * @since 1.0.0/public void remove (constantdatadto mo
del) {list<constantdatadto> dtolist = Cache.get (Model.getcodeset ());
if (null!= dtolist) {if (Dtolist.size () > 1) {dtolist.remove (model);
Cache.replace (Model.getcodeset (), dtolist);
else {cache.remove (Model.getcodeset ()); }}/** * Destroy cache * Destory * void * @since 1.0.0
* * @PreDestroy public void Destory () {cache.clear ();
}
}
3. Implementing the Initializingbean interface allows the spring container to load data at startup, enabling the Servletcontextaware interface to get ServletContext through AOP, This enables the cache to be placed in the application.
Systemdatabean.java
Import Javax.servlet.ServletContext;
Import Javax.sql.DataSource;
Import Org.springframework.beans.factory.InitializingBean;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Org.springframework.web.context.ServletContextAware; /** * Cachebean.java * @author Steveguoshao * @created July 10, 2014 afternoon 3:36:13 * @version 1.0/@Component Pub
Lic class Systemdatabean implements Initializingbean,servletcontextaware {private ServletContext servletcontext;
@Override public void Setservletcontext (ServletContext servletcontext) {this.servletcontext = ServletContext;
@Autowired private Constantdatacache Datacache;
@Autowired private DataSource DataSource; @Override public void Afterpropertiesset () throws Exception {//cache into ServletContext Servletcontext.setattribute (Con Stants.
CACHE, Datacache.getcache ());
Servletcontext.setattribute (Constants.data_source, DataSource);
}
}