默认缓存管理器 Spring 使用@EnableCaching 启动
Default Cache Manager with Spring Boot using @EnableCaching
我已经在 SpringBootApplication 中实现了缓存,如下所示
@SpringBootApplication
@EnableCaching
public class SampleApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
这绝对工作正常。
但是要实现缓存,应该定义一个强制性的 CacheManager / Cacheprovider。
在没有定义任何 cacheManager 的情况下,我的应用程序也能正常工作。
是否有 Spring 定义的任何默认缓存管理器?
Spring 文档说 Spring 引导自动配置合适的 CacheManager。
那么如果不定义CacheManager会用到什么?
Spring 启动器提供 a simple cache provider which stores values in an instance of ConcurrentHashMap。这是缓存机制最简单的 thread-safe 实现。
如果您的应用程序中存在 @EnableCaching
注释,Spring Boot 会检查您的 class 路径上可用的依赖项并配置适当的 CacheManager
。根据所选的提供商,可能需要一些额外的配置。您可以从这个答案的第一个 link 中找到有关配置的所有信息。
如果您想(出于任何原因)明确定义最简单的缓存管理器(在后台使用 ConcurrentHashMap),请执行以下操作:
@Bean
public CacheManager cacheManager() {
return new org.springframework.cache.concurrent.ConcurrentMapCacheManager();
}
我已经在 SpringBootApplication 中实现了缓存,如下所示
@SpringBootApplication
@EnableCaching
public class SampleApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
这绝对工作正常。
但是要实现缓存,应该定义一个强制性的 CacheManager / Cacheprovider。 在没有定义任何 cacheManager 的情况下,我的应用程序也能正常工作。
是否有 Spring 定义的任何默认缓存管理器? Spring 文档说 Spring 引导自动配置合适的 CacheManager。
那么如果不定义CacheManager会用到什么?
Spring 启动器提供 a simple cache provider which stores values in an instance of ConcurrentHashMap。这是缓存机制最简单的 thread-safe 实现。
如果您的应用程序中存在 @EnableCaching
注释,Spring Boot 会检查您的 class 路径上可用的依赖项并配置适当的 CacheManager
。根据所选的提供商,可能需要一些额外的配置。您可以从这个答案的第一个 link 中找到有关配置的所有信息。
如果您想(出于任何原因)明确定义最简单的缓存管理器(在后台使用 ConcurrentHashMap),请执行以下操作:
@Bean
public CacheManager cacheManager() {
return new org.springframework.cache.concurrent.ConcurrentMapCacheManager();
}