springboot的原生cache_使用@EnableCaching的Spring Boot默认缓存管理器(Default Cache Manager with Spring Boot using ...

本文围绕Spring Boot中缓存管理器展开。在Spring Boot应用里实现缓存时,即便未定义CacheManager应用也能正常工作。探讨了Spring是否有默认缓存管理器,还介绍了Spring Boot启动程序提供的简单缓存提供程序,以及Spring AOP基于代理的相关情况等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用@EnableCaching的Spring Boot默认缓存管理器(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 Boot自动配置一个合适的CacheManager。

那么如果我们不定义CacheManager会使用什么呢?

I have implemented caching in my SpringBootApplication as shown below

@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);

}

This is absolutely working fine.

But to implement caching there should be one mandatory CacheManager / Cacheprovider defined. Without defining any cacheManager also my application is working fine.

Is there any default Cache manager defined by Spring ? Spring docs says Spring Boot auto-configures a suitable CacheManager.

So what will be CacheManager used if we do not define it ?

原文:https://stackoverflow.com/questions/41739581

更新时间:2020-02-17 18:07

相关问答

好的,我按照本教程解决了这个问题。 这就是我所做的。 我创建了缓存配置类,它将定义我在JBoss中配置的CacheManager: @Configuration

@EnableCaching

public class CachingConfig {

@Bean

public CacheManager cacheManager() {

JndiTemplate jndiTemplate = new JndiTemplate();

try {

...

@ Jaiwo99是对的。 Spring的 Cache Abstraction不处理特定的语义和“管理”缓存内容的“低级”细节(例如大小,或类似相关,驱逐/过期)。 这在很大程度上是因为这些低级管理细节从1个缓存提供者到下一个缓存提供者差别很大。 例如,一些高速缓存提供程序/实现是高度分布式的,具有不同的一致性策略,冗余和控制延迟的机制等等。 因此,如果某些提供商甚至没有实现所述功能,或者具有非常不同的“一致性”策略等,则在这些功能之上提供一致的抽象是非常困难的。 无论如何,Spring参考指南中

...

如果bean使用@Transactional或@Cacheable注释,则默认情况下Spring会生成JDK动态代理以支持AOP。 动态代理类( com.sun.proxy.$Proxy61 )继承/实现目标bean实现的所有接口。 如果目标bean缺少接口,则代理类不会实现接口。 但是,Spring框架可以使用cglib生成一个特殊的代理类(缺少一个接口),它继承自原始类并在子方法中添加行为。 由于您的Service类没有实现任何接口,因此Spring框架生成一个没有任何接口的合成代理类(com

...

有两个相同名称的对象 http://docs.hazelcast.org/docs/3.8/javadoc/com/hazelcast/cache/HazelcastCacheManager.html是一个界面。 http://docs.hazelcast.org/docs/3.8/javadoc/com/hazelcast/spring/cache/HazelcastCacheManager.html是一个类 这个例子https://github.com/hazelcast/hazelcast-

...

Spring Boot启动程序提供了一个简单的缓存提供程序 ,它将值存储在ConcurrentHashMap的实例中。 这是缓存机制最简单的线程安全实现。 如果应用程序中存在@EnableCaching批注,Spring Boot会检查类路径上可用的依赖关系并配置适当的CacheManager 。 根据所选的提供商,可能需要一些其他配置。 您可以在此答案的第一个链接中找到有关配置的所有信息。 The Spring Boot starter provides a simple cache provi

...

TL; DR Spring AOP是基于代理的 ,因此当您从getTest()方法调用printTest()方法时,将在this引用上调用printTest()方法,而不是能够执行缓存操作的代理版本。 通常这是一种设计气味 ,您最好重新考虑当前的设计。 但作为一种解决方法,您可以使用AopContext : public void printTest() {

System.out.println(((TestRepository) AopContext.currentProxy()).ge

...

直接的方法是通过在Anypoint API管理器中生成代理来代理您的自定义API。 在CloudHub中的Mule服务器上或在内部运行时上部署该代理应用程序。 然后,您可以控制Spring Boot Application API,并可以应用策略,查看分析等。 API代理上的MuleSoft Doc The straight forward way is to proxy your custom API by generating a proxy in the Anypoint API Manag

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值