`
matt.u
  • 浏览: 127242 次
  • 性别: Icon_minigender_1
  • 来自: CQ
社区版块
存档分类
最新评论

通过注解将Spring和Ehcache集成

阅读更多
    以前有个项目springmodules,里面有个cache模块可以集成Spring和多种Cache,很可以这个项目只支持Spring2.5.x,而且没怎么更新了,代码库也从dev.java.net转到了github。

    无意中发现了hcache-spring-annotations项目,地址http://code.google.com/p/ehcache-spring-annotations/,可以通过注解让应用透明支持缓存,虽然目前只能用ehcache,但一定范围内也足够使用了,配置很简单,详情可见其网站。

    不过配置完后老是不生效,最后才发现问题:
    1、ehcache:annotation-driven只查找本配置文件当中的bean里面是否有Cachable(未测试import配置文件的情况)。
    2、使用ehcache:annotation-driven后,bean里面的自动注解会失效。
分享到:
评论
9 楼 awdxzc 2011-03-14  
servlet里面的方法不能用这种方式被缓存吧? 不是要被AOP拦截吗?
8 楼 awdxzc 2011-01-24  
matt.u 写道
虽然你的service是被spring管理了,但是由于你是Service自己调自己的方法,所以此时应该直接使用的this来调用方法,而没有使用代理,所以无法使用缓存。

Controller调Service的情况就不一样了,Service是由Spring注入的,所以可以被aop。

谢谢 了解其中原因了。获益良多。谢谢
7 楼 matt.u 2011-01-13  
虽然你的service是被spring管理了,但是由于你是Service自己调自己的方法,所以此时应该直接使用的this来调用方法,而没有使用代理,所以无法使用缓存。

Controller调Service的情况就不一样了,Service是由Spring注入的,所以可以被aop。
6 楼 awdxzc 2011-01-12  
matt.u 写道
哦。想起一个限制条件。因为该项目是利用aop机制和代理机制来为方法添加缓存,所以要看你想缓存的方法是否在spring的管理范围内。


这2个方法都是在service层,一个方法在另外一个方法里面被循环调用罢了,我想让assemblePeerForOtherFields被循环调用的时候实现注释形式缓存。可是不行,只能在其调用方法assemblePeersForOtherFields(多一个s,命名不是很好)的时候给缓存,而这个方法是controller层调用的。奇怪的事情发生了,如果我在cotroller层循环调用assemblePeerForOtherFields 就能实现缓存。在assemblePeersForOtherFields(在service层)循环调用就无法实现缓存。 不知道为什么
5 楼 matt.u 2011-01-08  
哦。想起一个限制条件。因为该项目是利用aop机制和代理机制来为方法添加缓存,所以要看你想缓存的方法是否在spring的管理范围内。
4 楼 matt.u 2011-01-08  
按照理论说在assemblePeerForOtherFields方法上应该没有问题。你只有自己调试下。
我感觉这个项目还有点不太成熟,偶尔会碰到些问题,但是并没给出警告或者提示信息。需要自己去调试。
3 楼 awdxzc 2011-01-06  
对于对象怎么缓存?
2 楼 awdxzc 2011-01-06  
还有回答你的问题import导入的是有效的  我就不明白缓存难道在方法里面被循环调用就不能起作用?
1 楼 awdxzc 2011-01-06  
请问个问题,为什么
@Cacheable(cacheName = "webserviceCache", keyGeneratorName="infoKeyGenerator")
	public List<Competitors> assemblePeersForOtherFields(List<Competitors> nativeCompetitorsList,String region)
	{
List<Competitors> resultCompetitorss = new ArrayList<Competitors>();
for(Competitors competitor : nativeCompetitorsList)
competitor = assemblePeerForOtherFields(competitor,region);
resultCompetitorss.add(competitor);
		}
		
		return resultCompetitorss;
	}

这样就能被缓存执行,而我的缓存建立在方法assemblePeerForOtherFields上就不行呢?
@Cacheable(cacheName = "webserviceCache", keyGeneratorName="infoKeyGenerator")
	public Competitors assemblePeerForOtherFields(Competitors competitor,String region)
	{ 。。。。。。}

相关推荐

Global site tag (gtag.js) - Google Analytics