spring 3.2.4配置注解ehcache
程序员文章站
2022-03-17 16:25:34
...
配置ehcache,ehcache.xml内容如下:
配置spring管理ehcache:
重点在上面配置里的那句,【缓存配置一定要声明在spring主配置文件中才会生效】搞了我一晚上。
现在只是写来总结一下。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="userCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="100000" timeToLiveSeconds="100000" overflowToDisk="false" /> </ehcache>
配置spring管理ehcache:
<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:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <!--启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效--> <cache:annotation-driven cache-manager="cacheManager" /> <!-- cacheManager工厂类,指定ehcache.xml的位置 --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" /> <!-- 声明cacheManager --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="cacheManagerFactory" /> </beans>
public class EhcacheSpringTest { private CacheManager cacheManager; private Cache cache; @Before public void init() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); EhCacheCacheManager ehCacheManager = (EhCacheCacheManager) ctx .getBean("cacheManager"); cacheManager = ehCacheManager.getCacheManager(); cache = cacheManager.getCache("userCache"); } @After public void destory() { cacheManager.shutdown(); } @Test public void testEhcacheString() { String key = "hello"; String value = "world"; cache.put(new Element(key, value)); Element element = cache.get(key); Object obj = element.getObjectValue(); Assert.assertEquals(value, obj); System.out.println(obj); } @Test public void testEhcacheObj() { String key = "user"; User value = new User("zhangsan", "lisi"); cache.put(new Element(key, value)); Element element = cache.get(key); Object obj = element.getObjectValue(); Assert.assertEquals(value, obj); System.out.println(obj); Object objSer = element.getValue(); Assert.assertEquals(value, objSer); System.out.println(objSer); } }
重点在上面配置里的那句,【缓存配置一定要声明在spring主配置文件中才会生效】搞了我一晚上。
现在只是写来总结一下。
推荐阅读
-
荐 Spring-boot-study02-spring.xml配置文件注入组件和@Bean注解注入组件差别
-
Spring的IOC控制反转实现注解方式配置
-
Spring纯注解配置实现代码示例解析
-
spring-AOP(面向切面编程)-注解方式配置
-
Spring5参考指南:基于注解的容器配置
-
spring之aop基于注解开发配置
-
spring配置和注解事务同时存在导致的事务嵌套
-
spring配置和注解事务同时存在导致的事务嵌套
-
Spring Ioc与Aop注解方式配置
-
spring boot中关于获取配置文件注解的使用@ConfigurationProperties、@Value、@PropertySource