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

Spring 5 cache not work for @Cachable (CacheInterceptor is not in call stack)?

我通过添加 @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.

在我的某些代码中,私有方法将被缓存。所以我必须将 AspectJ 与加载时编织一起使用。

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

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

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

<context:load-time-weaver/>

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