Springcache 仅适用于 XML 配置

Springcache not working only with XML configuration

我正在使用 XML 配置实现 Springcache,并希望删除所有注释。我只想替换 applicationContext.xml 文件中的注释。这是我的代码 -

//DummyBean.java
package com.spring.example;
interface DummyBean {
    public String getCity();
}

//DummyBeanImpl.java
package com.spring.example;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@CacheConfig(cacheNames={"myCache"})
public class DummyBeanImpl implements DummyBean {

    private String city = "New York";

    @Cacheable()
    public String getCity() {
        System.out.println("DummyBean.getCity() called!");
        return city;
    }
}

SpringAwareAnnotationXMLConfig.java

package com.spring.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class SpringAwareAnnotationXMLConfig {

  public static void main(String[] args) throws Exception {
    AbstractApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");

    DummyBean personService = (DummyBean) context.getBean("myBean");
    System.out.println(personService.getCity());
    System.out.println(personService.getCity());
    System.out.println(personService.getCity());

    ((AbstractApplicationContext) context).close();
  }
}

applicationContext.xml

<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:cache="http://www.springframework.org/schema/cache"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                               http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
                               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd>


  <context:annotation-config/>
  <cache:annotation-driven cache-manager="cacheManager"/>
  <context:component-scan base-package="com.spring"/>

  <bean id="myBean" class="com.spring.example.DummyBeanImpl"/>

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

  <cache:advice id="cacheAdvice" cache-manager="cacheManager">
    <cache:caching cache="myCache">
      <cache:cacheable method="getCity"/>
    </cache:caching>
  </cache:advice>
</beans>

如您所见,如果我在 Java 代码中保留 @EnableCaching、@CacheConfig、@Cacheable 注释,则缓存工作正常并且我得到以下输出 -

DummyBean.getCity() called!
New York
New York
New York

但是如果我删除注释,则会得到以下输出 -

DummyBean.getCity() called!
New York
DummyBean.getCity() called!
New York
DummyBean.getCity() called!
New York

这意味着缓存不起作用!

但我的期望是在 applicationContext.xml 文件中我已经通过 <cache:annotation-driven cache-manager="cacheManager"/> 启用了缓存,我已经提到了缓存名称并且我已经使用缓存的方法名称配置了缓存是要实施的。因此,如果我省略注释,我应该会得到所需的输出。但是为什么我没有收到?

谢谢 尼尔玛利亚

使用 XML 配置方面时,您还需要添加 aop:config 部分以应用方面。

<aop:config>
    <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.spring.example.DummyBean+.*(..))"/>
</aop:config>

假设 DummyBean 是由 com.spring.example.DummyBeanImpl 实现的接口,这应该可以解决问题。这说明您要将 cacheAdvice(方面)应用于匹配表达式的 bean。

另见 reference guide