简体   繁体   English

Spring Cache:如何为应用程序指定全局@CacheConfig

[英]Spring Cache: How to specify global @CacheConfig for application

I am working on setting up Spring Cache support for my Java application. 我正在为我的Java应用程序设置Spring Cache支持。 I want a way to set a global @CacheConfig for my entire project because I want all cache operations in the application to use the exact same cache name and custom KeyGenerator implementation . 我想要一种为整个项目设置全局@CacheConfig的方法,因为我希望应用程序中的所有缓存操作都使用完全相同的缓存名称自定义KeyGenerator实现 I was hoping that I could annotate some global configuration class or my CacheManager with @CacheConfig , but it looks like @CacheConfig is only meant as configuration for the class of the method with the Cache Annotation. 我希望我可以使用@CacheConfig注释某些全局配置类或CacheManager ,但是看起来@CacheConfig仅意味着使用Cache Annotation进行方法类的配置。 Thus, I would need to use the same exact @CacheConfig annotation definition for every class using Spring Cache operations. 因此,对于使用Spring Cache操作的每个类,我将需要使用完全相同的@CacheConfig批注定义。

A workaround that I'm going to use in the meanwhile is implement my own meta-annotation of Spring Cache annotations such as @Cacheable , so that the same exact name and keyGenerator will be used for all cache operations. 同时,我要使用的一种解决方法是实现自己的Spring Cache注释(例如@Cacheable元注释 ,以便所有缓存操作都使用相同的确切namekeyGenerator But, there must be a better way to do this than using custom annotations. 但是,必须有比使用自定义注释更好的方法。 If I ever run into the situation where I want to use a Cache named something other than "default", then I'm going to run into trouble with my custom annotation. 如果我曾经遇到过要使用一个名为“ default”以外的名称的Cache的情况,那么我的自定义注释会遇到麻烦。

Any ideas? 有任何想法吗? Thank you. 谢谢。

Yes, this is doable. 是的,这是可行的。

All you need is to populate AnnotationCacheOperationSource with your own CacheAnnotationParser , see how is it done by default: SpringCacheAnnotationParser . 您所需要做的就是用您自己的CacheAnnotationParser填充AnnotationCacheOperationSource ,看看默认情况下它是如何完成的: SpringCacheAnnotationParser

Unfortunately I can't provide you with code snippets, my Spring-fu is too weak at this moment. 不幸的是,我无法为您提供代码片段,我的Spring-fu目前太弱了。 If you manage to get this working - let me know. 如果您设法解决此问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以定义通用接口并使用Spring @CacheConfig? - Is it possible to define generic interface and use Spring @CacheConfig? 如何在spring java应用程序中缓存图像文件? - How to cache image files in spring java application? Spring集成应用程序和缓存 - Spring integration application and cache Spring 反应式应用程序中的缓存 - Spring Cache in a reactive application 如何在Spring启动应用程序中启动应用程序期间缓存数据 - How to cache data during application startup in Spring boot application 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? 如何在 application.yml Spring Cloud Gateway 中指定自定义过滤器 - How to Specify custom filter in application.yml Spring Cloud Gateway 如何制作缓存取决于Spring应用程序启动时的其他缓存 - How to make cache depends on other caches on startup of spring application 如何使用 Thymleaf 在 Spring Boot 应用程序中正确缓存数据 - How to properly cache data in spring boot application with Thymleaf 如何在HTTP响应中进行单元测试“缓存控制”-Spring MVC Application - How to unit test “cache control” in http response - spring mvc application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM