spring注解方式实现Cache的简单Demo
2017-06-12 10:17
459 查看
第一步:加入所依赖的jar包
ehcache.xml配置文件如下所示:
第四步:在xxx-service.xml文件中加入如下配置:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
第5步:在需要缓存的service或者biz的方法上加上对应的注:
<!--Ehcache--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.6</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.8.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency>第二步: 加入ehcache.xml和ehcache.xsd配置文件
ehcache.xml配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="classpath:/ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir/" /> <!-- diskStore path:用来配置磁盘缓存使用的物理路径 name: 缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里) eternal="false" 元素是否永恒,如果是就永不过期(必须设置) maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大 maxElementsInMemory="1000" 内存缓存中最多可以存放的元素数量(必须设置) timeToIdleSeconds="0" 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0 timeToLiveSeconds="600" 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期 overflowToDisk="false" 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置) diskPersistent="false" 磁盘缓存在VM重新启动时是否保持(默认为false) diskExpiryThreadIntervalSeconds="100" 磁盘失效线程运行时间间隔,默认是120秒 memoryStoreEvictionPolicy="LFU" 内存存储与释放策略.当达到maxElementsInMemory时 共有三种策略,分别为LRU(最近最少使用,最久未使用算法,使用时间距离现在最久的那个被移除;)、LFU(最常用的,最近最少使用算法,一定时间段内使用次数(频率)最少的那个被移除)、FIFO(先进先出)默认使用"最近使用"策略 --> <defaultCache eternal="false" maxElementsInMemory="3000" timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="100" memoryStoreEvictionPolicy="LRU"/> <cache name="propConfigCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="workTimeCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="threeInOneCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="transferCache" eternal="false" maxElementsInMemory="1000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="threeInOneFavCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="reserveTimeCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="mqServerNameCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> <cache name="schWorkTimeCache" eternal="false" maxElementsInMemory="3000" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/> </ehcache>ehcache.xsd的配置文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.7"> <xs:element name="ehcache"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="1" minOccurs="0" ref="diskStore"/> <xs:element maxOccurs="1" minOccurs="0" ref="sizeOfPolicy"/> <xs:element maxOccurs="1" minOccurs="0" ref="transactionManagerLookup"/> <xs:element maxOccurs="1" minOccurs="0" ref="cacheManagerEventListenerFactory"/> <xs:element maxOccurs="1" minOccurs="0" ref="managementRESTService"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="cacheManagerPeerProviderFactory"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="cacheManagerPeerListenerFactory"/> <xs:element maxOccurs="1" minOccurs="0" ref="terracottaConfig"/> <xs:element maxOccurs= "1" minOccurs="0" ref="defaultCache"/> <xs:element maxOccurs="unbounded" minOccurs="0" ref="cache"/> </xs:sequence> <xs:attribute name="name" use="optional"/> <xs:attribute default="true" name="updateCheck" type="xs:boolean" use="optional"/> <xs:attribute default="autodetect" name="monitoring" type="monitoringType" use="optional"/> <xs:attribute default="true" name="dynamicConfig" type="xs:boolean" use="optional"/> <xs:attribute default="15" name="defaultTransactionTimeoutInSeconds" type="xs:integer" use="optional"/> <xs:attribute default="0" name="maxBytesLocalHeap" type="memoryUnitOrPercentage" use="optional"/> <xs:attribute default="0" name="maxBytesLocalOffHeap" type="memoryUnit" use="optional"/> <xs:attribute default="0" name="maxBytesLocalDisk" type="memoryUnit" use="optional"/> </xs:complexType> </xs:element> <xs:element name="managementRESTService"> <xs:complexType> <xs:attribute name="enabled" type="xs:boolean" use="optional"/> <xs:attribute name="bind" use="optional"/> <xs:attribute name="securityServiceLocation" use="optional"/> <xs:attribute name="securityServiceTimeout" use="optional" type="xs:integer"/> <xs:attribute name="sslEnabled" use="optional" type="xs:boolean"/> <xs:attribute name="needClientAuth" use="optional" type="xs:boolean"/> <xs:attribute name="sampleHistorySize" use="optional" type="xs:integer"/> <xs:attribute name="sampleIntervalSeconds" use="optional" type="xs:integer"/> <xs:attribute name="sampleSearchIntervalSeconds" use="optional" type="xs:integer"/> </xs:complexType> </xs:element> <xs:element name="diskStore"> <xs:complexType> <xs:attribute name="path" use="optional"/> </xs:complexType> </xs:element> <xs:element name="transactionManagerLookup"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerEventListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerPeerProviderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerPeerListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="terracottaConfig"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="1" minOccurs="0" name="tc-config"> <xs:complexType> <xs:sequence> <xs:any maxOccurs="unbounded" minOccurs="0" processContents="skip"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute default="localhost:9510" name="url" use="optional"/> <xs:attribute name="rejoin" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="wanEnabledTSA" type="xs:boolean" use="optional" default="false"/> </xs:complexType> </xs:element> <!-- add clone support for addition of cacheExceptionHandler. Important! --> <xs:element name="defaultCache"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheExtensionFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheDecoratorFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="bootstrapCacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheExceptionHandlerFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="pinning"/> <xs:element minOccurs="0" maxOccurs="1" ref="terracotta"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheWriter"/> <xs:element minOccurs="0" maxOccurs="1" ref="copyStrategy"/> <xs:element minOccurs="0" maxOccurs="1" ref="elementValueComparator"/> <xs:element minOccurs="0" maxOccurs="1" ref="sizeOfPolicy"/> <xs:element minOccurs="0" maxOccurs="1" ref="persistence"/> </xs:sequence> <xs:attribute name="diskExpiryThreadIntervalSeconds" type="xs:integer" use="optional"/> <xs:attribute name="diskSpoolBufferSizeMB" type="xs:integer" use="optional"/> <xs:attribute name="diskPersistent" type="xs:boolean" use="optional"/> <xs:attribute name="diskAccessStripes" type="xs:integer" use="optional" default="1"/> <xs:attribute name="eternal" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxElementsInMemory" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalHeap" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="clearOnFlush" type="xs:boolean" use="optional"/> <xs:attribute name="memoryStoreEvictionPolicy" type="xs:string" use="optional"/> <xs:attribute name="overflowToDisk" type="xs:boolean" use="optional"/> <xs:attribute name="timeToIdleSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="timeToLiveSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxElementsOnDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="transactionalMode" type="transactionalMode" use="optional" default="off"/> <xs:attribute name="statistics" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnRead" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnWrite" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="cacheLoaderTimeoutMillis" type="xs:integer" use="optional" default="0"/> <xs:attribute name="overflowToOffHeap" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxMemoryOffHeap" type="xs:string" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cache"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheExtensionFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheDecoratorFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="bootstrapCacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheExceptionHandlerFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="pinning"/> <xs:element minOccurs="0" maxOccurs="1" ref="terracotta"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheWriter"/> <xs:element minOccurs="0" maxOccurs="1" ref="copyStrategy"/> <xs:element minOccurs="0" maxOccurs="1" ref="searchable"/> <xs:element minOccurs="0" maxOccurs="1" ref="elementValueComparator"/> <xs:element minOccurs="0" maxOccurs="1" ref="sizeOfPolicy"/> <xs:element minOccurs="0" maxOccurs="1" ref="persistence"/> </xs:sequence> <xs:attribute name="diskExpiryThreadIntervalSeconds" type="xs:integer" use="optional"/> <xs:attribute name="diskSpoolBufferSizeMB" type="xs:integer" use="optional"/> <xs:attribute name="diskPersistent" type="xs:boolean" use="optional"/> <xs:attribute name="diskAccessStripes" type="xs:integer" use="optional" default="1"/> <xs:attribute name="eternal" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxElementsInMemory" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalHeap" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="memoryStoreEvictionPolicy" type="xs:string" use="optional"/> <xs:attribute name="clearOnFlush" type="xs:boolean" use="optional"/> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="overflowToDisk" type="xs:boolean" use="optional"/> <xs:attribute name="timeToIdleSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="timeToLiveSeconds" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxElementsOnDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesLocalDisk" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="maxEntriesInCache" type="xs:nonNegativeInteger" use="optional"/> <xs:attribute name="transactionalMode" type="transactionalMode" use="optional" default="off" /> <xs:attribute name="statistics" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnRead" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="copyOnWrite" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="logging" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="cacheLoaderTimeoutMillis" type="xs:integer" use="optional" default="0"/> <xs:attribute name="overflowToOffHeap" type="xs:boolean" use="optional" default="false"/> <xs:attribute name="maxMemoryOffHeap" type="xs:string" use="optional"/> <xs:attribute default="0" name="maxBytesLocalHeap" type="memoryUnitOrPercentage" use="optional"/> <xs:attribute default="0" name="maxBytesLocalOffHeap" type="memoryUnitOrPercentage" use="optional"/> <xs:attribute default="0" name="maxBytesLocalDisk" type="memoryUnitOrPercentage" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheEventListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> <xs:attribute name="listenFor" use="optional" type="notificationScope" default="all"/> </xs:complexType> </xs:element> <xs:element name="bootstrapCacheLoaderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheExtensionFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheExceptionHandlerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheLoaderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheDecoratorFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="searchAttribute"> <xs:complexType> <xs:attribute name="name" use="required" type="xs:string" /> <xs:attribute name="expression" type="xs:string" /> <xs:attribute name="class" type="xs:string" /> <xs:attribute name="type" type="xs:string" use="optional"/> <xs:attribute name="properties" use="optional" /> <xs:attribute name="propertySeparator" use="optional" /> </xs:complexType> </xs:element> <xs:element name="searchable"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="searchAttribute"/> </xs:sequence> <xs:attribute name="keys" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="values" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="allowDynamicIndexing" use="optional" type="xs:boolean" default="false"/> </xs:complexType> </xs:element> <xs:element name="pinning"> <xs:complexType> <xs:attribute name="store" use="required" type="pinningStoreType"/> </xs:complexType> </xs:element> <xs:element name="terracotta"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="nonstop"/> </xs:sequence> <xs:attribute name="clustered" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="coherentReads" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="localKeyCache" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="localKeyCacheSize" use="optional" type="xs:positiveInteger" default="300000"/> <xs:attribute name="orphanEviction" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="orphanEvictionPeriod" use="optional" type="xs:positiveInteger" default="4"/> <xs:attribute name="copyOnRead" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="coherent" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="consistency" use="optional" type="consistencyType" default="eventual"/> <xs:attribute name="synchronousWrites" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="concurrency" use="optional" type="xs:nonNegativeInteger" default="0"/> <xs:attribute name="localCacheEnabled" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="compressionEnabled" use="optional" type="xs:boolean" default="false"/> </xs:complexType> </xs:element> <xs:simpleType name="consistencyType"> <xs:restriction base="xs:string"> <xs:enumeration value="strong" /> <xs:enumeration value="eventual" /> </xs:restriction> </xs:simpleType> <xs:element name="nonstop"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="timeoutBehavior"/> </xs:sequence> <xs:attribute name="enabled" use="optional" type="xs:boolean" default="true"/> <xs:attribute name="immediateTimeout" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="timeoutMillis" use="optional" type="xs:positiveInteger" default="30000"/> <xs:attribute name="searchTimeoutMillis" use="optional" type="xs:positiveInteger" default="30000"/> </xs:complexType> </xs:element> <xs:element name="timeoutBehavior"> <xs:complexType> <xs:attribute name="type" use="optional" type="timeoutBehaviorType" default="exception"/> <xs:attribute name="properties" use="optional" default=""/> <xs:attribute name="propertySeparator" use="optional" default=","/> </xs:complexType> </xs:element> <xs:simpleType name="timeoutBehaviorType"> <xs:restriction base="xs:string"> <xs:enumeration value="noop" /> <xs:enumeration value="exception" /> <xs:enumeration value="localReads" /> <xs:enumeration value="localReadsAndExceptionOnWrite" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="monitoringType"> <xs:restriction base="xs:string"> <xs:enumeration value="autodetect"/> <xs:enumeration value="on"/> <xs:enumeration value="off"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="pinningStoreType"> <xs:restriction base="xs:string"> <xs:enumeration value="localMemory" /> <xs:enumeration value="inCache" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="terracottaCacheValueType"> <xs:restriction base="xs:string"> <xs:enumeration value="serialization" /> <xs:enumeration value="identity" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="transactionalMode"> <xs:restriction base="xs:string"> <xs:enumeration value="off"/> <xs:enumeration value="xa_strict"/> <xs:enumeration value="xa"/> <xs:enumeration value="local"/> </xs:restriction> </xs:simpleType> <xs:element name="cacheWriter"> <xs:complexType> <xs:sequence > <xs:element minOccurs="0" maxOccurs="1" ref="cacheWriterFactory"/> </xs:sequence> <xs:attribute name="writeMode" use="optional" type="writeModeType" default="write-through"/> <xs:attribute name="notifyListenersOnException" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="minWriteDelay" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="maxWriteDelay" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="rateLimitPerSecond" use="optional" type="xs:nonNegativeInteger" default="0"/> <xs:attribute name="writeCoalescing" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="writeBatching" use="optional" type="xs:boolean" default="false"/> <xs:attribute name="writeBatchSize" use="optional" type="xs:positiveInteger" default="1"/> <xs:attribute name="retryAttempts" use="optional" type="xs:nonNegativeInteger" default="0"/> <xs:attribute name="retryAttemptDelaySeconds" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="writeBehindConcurrency" use="optional" type="xs:nonNegativeInteger" default="1"/> <xs:attribute name="writeBehindMaxQueueSize" use="optional" type="xs:nonNegativeInteger" default="0"/> </xs:complexType> </xs:element> <xs:simpleType name="writeModeType"> <xs:restriction base="xs:string"> <xs:enumeration value="write-through" /> <xs:enumeration value="write-behind" /> </xs:restriction> </xs:simpleType> <xs:element name="cacheWriterFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="copyStrategy"> <xs:complexType> <xs:attribute name="class" use="required" type="xs:string" /> </xs:complexType> </xs:element> <xs:element name="elementValueComparator"> <xs:complexType> <xs:attribute name="class" use="required" type="xs:string" /> </xs:complexType> </xs:element> <xs:element name="sizeOfPolicy"> <xs:complexType> <xs:attribute name="maxDepth" use="required" type="xs:integer" /> <xs:attribute name="maxDepthExceededBehavior" use="optional" default="continue" type="maxDepthExceededBehavior" /> </xs:complexType> </xs:element> <xs:element name="persistence"> <xs:complexType> <xs:attribute name="strategy" use="required" type="persistenceStrategy"/> <xs:attribute name="synchronousWrites" use="optional" default="false" type="xs:boolean"/> </xs:complexType> </xs:element> <xs:simpleType name="persistenceStrategy"> <xs:restriction base="xs:string"> <xs:enumeration value="localTempSwap"/> <xs:enumeration value="localRestartable"/> <xs:enumeration value="none"/> <xs:enumeration value="distributed"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="maxDepthExceededBehavior"> <xs:restriction base="xs:string"> <xs:enumeration value="continue"/> <xs:enumeration value="abort"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="notificationScope"> <xs:restriction base="xs:string"> <xs:enumeration value="local"/> <xs:enumeration value="remote"/> <xs:enumeration value="all"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="memoryUnit"> <xs:restriction base="xs:token"> <xs:pattern value="[0-9]+[bBkKmMgG]?"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="memoryUnitOrPercentage"> <xs:restriction base="xs:token"> <xs:pattern value="([0-9]+[bBkKmMgG]?|100%|[0-9]{1,2}%)"/> </xs:restriction> </xs:simpleType> </xs:schema>
第四步:在xxx-service.xml文件中加入如下配置:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="cacheManager"/> <!-- cacheManager工厂类,指定ehcache.xml的位置 --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:config/ehcache.xml"/> </bean> <!-- 声明cacheManager --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean>
第5步:在需要缓存的service或者biz的方法上加上对应的注:
@Cacheable(value = "propConfigCache",key = "#dateStr+'skynet'")
相关文章推荐
- spring的cache的注解方式的简单实用(具体步骤)
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- 一个简单的基于注解的 Controller (spring 2.5 annotation 方式实现mvc )
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- 一个简单的基于注解的 Controller (spring 2.5 annotation 方式实现mvc )
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- 一个简单的基于注解的 Controller (spring 2.5 annotation 方式实现mvc )
- Spring的AOP分为注解和配置两种方式实现
- 使用Spring的注解方式实现AOP的细节
- aop:aspectj-autoproxy,SpringAOP拦截Controller,Service实现日志管理(自定义注解方式)
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring定时任务之注解方式实现
- Quartz 集成Spring实现简单的任务调度Demo
- Spring AOP 注解方式实现的一些“坑”
- spring aop相关概念和xml与注解实现方式
- 使用Spring的注解方式实现AOP
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- spring系列(二)——AOP的两种实现方式(xml和注解)
- Jeecg中通过Spring_AOP+注解方式实现日志的管理