Java 编写自定义Spring@Cacheable注释
我目前正在编写一个自定义的@Cacheable注释,除了Spring的@Cacheable提供的元数据属性外,它还将包含其他元数据属性。然而,Spring需要知道如何解析这个新注释。我的理解是,我需要扩展和覆盖AnnotationCacheOperationsSource的determineCacheOperations(),以便可以使用相应的CacheAbleOperations类初始化新注释。这是正确的方法吗 问候, 史蒂夫要看情况而定 根据, 此类读取Spring的可缓存、CachePut和CacheExit 注释并将相应的缓存操作定义公开给 Spring的缓存基础设施。此类也可以用作基类 对于自定义CacheOperationSource 因此,如果您要求回答是/否,如果您可以扩展该类以实现CacheOperationSource的扩展行为,那么答案是肯定的 但是,determineCacheOperations()方法使用的是所有可用的CacheAnnotationParser。唯一默认的CacheAnnotationParser是。如果您有一个自定义类,只需让另一个类为您的注释实现CacheAnnotationParser。然后,Spring应该自动使用它。您可以查看SpringCacheAnnotationParser,了解它们是如何实现的Java 编写自定义Spring@Cacheable注释,java,spring,caching,Java,Spring,Caching,我目前正在编写一个自定义的@Cacheable注释,除了Spring的@Cacheable提供的元数据属性外,它还将包含其他元数据属性。然而,Spring需要知道如何解析这个新注释。我的理解是,我需要扩展和覆盖AnnotationCacheOperationsSource的determineCacheOperations(),以便可以使用相应的CacheAbleOperations类初始化新注释。这是正确的方法吗 问候, 史蒂夫要看情况而定 根据, 此类读取Spring的可缓存、CachePut
编辑:好吧,我错了,这会自动发生。我的下一个建议是
- 实现接口CacheAnnotationParser,就像您已经做的那样
- 扩展AnnotationCacheOperationSource,以便在内部解析器集合中除了Spring之外,还可以放置自己的CacheAnnotationParser
- 定义自定义AnnotationCacheOperationSource以使用与Spring one相同的id,因此它将覆盖Spring internal。如果id匹配,它应该完全覆盖SpringOne。这将类似于: