[英]Writing a custom Spring @Cacheable annotation
我目前正在编写一个自定义的@Cacheable注释,除了Spring的@Cacheable提供的那些之外,它还将采用额外的元数据属性。 但是,Spring需要知道如何解析这个新的注释。 我的理解是我需要扩展和覆盖AnnotationCacheOperationSource的determineCacheOperations(),以便可以使用从其初始化的适当的CacheableOperation类来解析新的注释。 这是正确的方法吗?
问候,
史蒂夫
要看。
根据AnnotationCacheOperationSource javadoc ,
该类读取Spring的Cacheable,CachePut和CacheEvict注释,并将相应的缓存操作定义公开给Spring的缓存基础结构。 此类还可以作为自定义CacheOperationSource的基类。
因此,如果您可以扩展该类以获得CacheOperationSource的扩展行为,那么如果您要求是/否答案,则答案是肯定的。
但是,determineCacheOperations()方法的作用是它使用所有可用的CacheAnnotationParsers。 唯一的默认CacheAnnotationParser是SpringCacheAnnotationParser 。 如果你有一个自定义的,只需要另一个类为你的注释实现CacheAnnotationParser。 然后Spring应该自动使用它。 您可以查看SpringCacheAnnotationParser 源代码 ,了解它们是如何做到的。
编辑:好的,我错了,因为这会自动发生。 我的下一个建议是
定义你的自定义AnnotationCacheOperationSource使用与Spring一样的id,因此它将覆盖Spring内部。 如果id匹配,它应该干净地覆盖Spring。 这将是这样的:
<bean id="annotationCacheOperationSource" class="com.company.YourCustomAnnotationCacheOperationSource" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.