xml代码
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache ="http://www.springframework.org/schema/cache"
xmlns:p ="http://www.springframework.org/schema/p"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" >
< cache:annotation-driven />
<!-- generic cache manager -->
< bean id ="cacheManager" class ="org.springframework.cache.support.SimpleCacheManager" >
< property name ="caches" >
< set >
< bean class ="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name ="default" />
< bean class ="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name ="sys_resource" />
</ set >
</ property >
</ bean >
</ beans >
java代码
xmlns:cache ="http://www.springframework.org/schema/cache"
xmlns:p ="http://www.springframework.org/schema/p"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" >
< cache:annotation-driven />
<!-- generic cache manager -->
< bean id ="cacheManager" class ="org.springframework.cache.support.SimpleCacheManager" >
< property name ="caches" >
< set >
< bean class ="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name ="default" />
< bean class ="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name ="sys_resource" />
</ set >
</ property >
</ bean >
</ beans >
package org.jeasyweb.framework.sys_user.service;
import java.util.Iterator;
import java.util.Set;
import org.jeasyweb.framework.sys_user.entity.SysResource;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
@ContextConfiguration({ "classpath:applicationContext-dataSource.xml",
"classpath:applicationContext-role.xml",
"classpath:applicationContext-cache.xml" })
public class UserServiceTest extends
AbstractTransactionalJUnit4SpringContextTests {
@Autowired
UserService userService;
@Test
public void testHello() {
Set<SysResource> resources = userService
.getResourceByUserId( new Long(1));
Iterator<SysResource> it = resources.iterator();
}
@Test
public void testCache() {
Set<SysResource> resources2 = userService.getResourceByUserId( new Long(
1));
}
}
service代码
import java.util.Iterator;
import java.util.Set;
import org.jeasyweb.framework.sys_user.entity.SysResource;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
@ContextConfiguration({ "classpath:applicationContext-dataSource.xml",
"classpath:applicationContext-role.xml",
"classpath:applicationContext-cache.xml" })
public class UserServiceTest extends
AbstractTransactionalJUnit4SpringContextTests {
@Autowired
UserService userService;
@Test
public void testHello() {
Set<SysResource> resources = userService
.getResourceByUserId( new Long(1));
Iterator<SysResource> it = resources.iterator();
}
@Test
public void testCache() {
Set<SysResource> resources2 = userService.getResourceByUserId( new Long(
1));
}
}
@Override
@Cacheable(value = "sys_resource", key = "'getResourceByUserId_' + #id")
public Set<SysResource> getResourceByUserId(Long id) {
//
.
本文转自博客园沉睡森林@漂在北京的博客,原文链接:spring3.1使用cache,如需转载请自行联系原博主。
@Cacheable(value = "sys_resource", key = "'getResourceByUserId_' + #id")
public Set<SysResource> getResourceByUserId(Long id) {
//

.