ehcache缓存方法返回结果(二)--annotation

缓存一个方法的返回值,除了上篇文章的AOP方式,也可以通过手动的在某个方法上面加一个annotation的方法来实现,比AOP更加灵活,比如有些方法命名没有规律,不好AOP。缺点是侵入了方法内部,要在方法上面写annotation。
实现方法是在基于上篇文章[url] http://lastsoul.iteye.com/admin/blogs/2246566[/url]的基础上,在spring-ehcache.xml上加上:
<!-- 打开注解驱动 -->
<cache:annotation-driven/>
<!-- spring的缓存管理器,他会调用ehcache的缓存管理器 -->
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="mycacheManager"/>


java代码:
@Cacheable(value="SimplePageCachingFilter")
	@Override
	public User findById(Integer id) {
		System.out.println(id);
		User user=new User();
		user.setId(System.currentTimeMillis()+"");
		user.setName("test");
		return user;
	}

其中SimplePageCachingFilter是ehcache.xml中的cachename.

猜你喜欢

转载自lastsoul.iteye.com/blog/2259446