gpt4 book ai didi

java - Spring 5缓存不适用于@Cachable(CacheInterceptor不在调用堆栈中)?

转载 作者:行者123 更新时间:2023-12-02 11:19:43 24 4
gpt4 key购买 nike

我通过添加 @Cachable 注释来启用公共(public)方法的缓存,如下所示:

@Cacheable(cacheNames = "saas_setting", //
key = "#key")
public Setting get(String key) { ... }

另一方面,我添加了cacheManager bean:

<bean id="cacheManager"
class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
name="saas_setting" />
</set>
</property>
</bean>

我启用AOP:

<aop:aspectj-autoproxy 
proxy-target-class="true"/>

然后启用缓存:

<cache:annotation-driven 
mode="aspectj"
proxy-target-class="true"/>

但是,结果不会被缓存,并且当从系统的其他部分调用该方法时,就会调用该方法。

我在方法中放置了一个制动点并检查调用堆栈:堆栈中没有CachInterceptor?!

<小时/>

编辑:

这是完整的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.3.xsd">

<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy
proxy-target-class="true"/>

<!-- Enable cache -->
<cache:annotation-driven
mode="aspectj"
proxy-target-class="true"/>


<bean id="cacheManager"
class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
name="saas_setting" />
</set>
</property>
</bean>

<bean name="applicationProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false" />
<property name="locations">
<list>
<value>resources/server.properties</value>
<value>resources/modules/*.properties</value>
<value>resources/jetty/*.properties</value>
<value>resources/db/#{systemProperties['db.dialect']}.properties
</value>
<value>resources/db/#{systemProperties['db.orm']}.properties</value>
</list>
</property>
</bean>
<import resource="../context/beans-*.xml" />
</beans>
<小时/>

编辑:

基于 Spring 文档:

The default advice mode for processing caching annotations is "proxy" which allows for interception of calls through the proxy only; local calls within the same class cannot get intercepted that way. For a more advanced mode of interception, consider switching to "aspectj" mode in combination with compile-time or load-time weaving.

在我的代码的某些部分,私有(private)方法将被缓存。所以我必须使用 AspectJ 进行加载时编织。

最佳答案

因为您使用的是aspectJ 缓存模式,所以您的类路径中需要spring-aspects.jar。目前尚不清楚为什么要使用aspectj代理而不是默认代理。

由于您使用aspectj模式作为代理,因此您还需要设置加载时编织。

可以使用以下方式启用加载时间编织:

<context:load-time-weaver/>

此外,您使用的方法必须来自具体类,因为您正在使用 proxy-target-class="true"

关于java - Spring 5缓存不适用于@Cachable(CacheInterceptor不在调用堆栈中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50021651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com