编写自定义Spring @Cacheable批注 [英] Writing a custom Spring @Cacheable annotation

查看:551
本文介绍了编写自定义Spring @Cacheable批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个自定义的 @Cacheable 批注,该批注除了Spring的 @Cacheable 提供的属性外,还将具有其他元数据属性.但是,Spring需要知道如何解析这个新注释.我的理解是,我需要扩展和覆盖 AnnotationCacheOperationSource determineCacheOperations() ,以便可以使用适当的语法来解析新的注释.从其初始化的 CacheableOperation 类.这是正确的进行方式吗?

I'm currently writing a custom @Cacheable annotation that will take additonal meta-data attributes in addition to those that Spring's @Cacheable provides. However, Spring would need to know how to parse this new annotation. My understanding is that I'd need to extend and override AnnotationCacheOperationSource's determineCacheOperations() so that the new annotation can be parsed with an appropriate CacheableOperation class initialized from it. Is this the correct way to proceed?

此致

史蒂夫

推荐答案

取决于.

按照

此类读取Spring的Cacheable,CachePut和CacheEvict 批注并将相应的缓存操作定义公开给 Spring的缓存基础结构.该课程也可以作为基础课程 自定义CacheOperationSource.

This class reads Spring's Cacheable, CachePut and CacheEvict annotations and exposes corresponding caching operation definition to Spring's cache infrastructure. This class may also serve as base class for a custom CacheOperationSource.

因此,如果您要回答是/否,是否可以针对CacheOperationSource的扩展行为扩展该类,则答案是肯定的.

So if you're asking for yes/no answer if you can extend that class for an extended behaviour of CacheOperationSource, the answer is yes.

但是,defineCacheOperations()方法的作用是使用所有可用的CacheAnnotationParsers.唯一的默认CacheAnnotationParser是

However, what determineCacheOperations() method does is that it uses all the available CacheAnnotationParsers. The only default CacheAnnotationParser is SpringCacheAnnotationParser. If you have a custom one, just have another class implementing CacheAnnotationParser for your annotation. Spring should then use it automatically. You can take a look at the SpringCacheAnnotationParser source code to see how they do it.

好的,我错了,因为这会自动发生.我的下一个建议是

ok, I was wrong in that this would happen automatically. My next suggestion is to

  • 实现接口CacheAnnotationParser,就像您显然已经做到的一样
  • 扩展AnnotationCacheOperationSource,以便在内部解析器集合中除了Spring 1之外还放置自己的CacheAnnotationParser
  • 定义自定义的AnnotationCacheOperationSource以使用与Spring 1相同的ID,因此它将覆盖Spring内部.如果id匹配,则应完全覆盖Spring 1.就像这样:

  • implement the interface CacheAnnotationParser, like you apparently already did
  • extend AnnotationCacheOperationSource so that you put your own CacheAnnotationParser in addition to Spring one in the internal parsers collection
  • define your custom AnnotationCacheOperationSource to use the same id as Spring one does, so it will override Spring internal. If id matches, it should override Spring one cleanly. This would be something like:

<bean id="annotationCacheOperationSource" class="com.company.YourCustomAnnotationCacheOperationSource" />

<bean id="annotationCacheOperationSource" class="com.company.YourCustomAnnotationCacheOperationSource" />

这篇关于编写自定义Spring @Cacheable批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆