首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Springcache不仅适用于XML配置

Springcache不仅适用于XML配置
EN

Stack Overflow用户
提问于 2019-03-25 07:42:09
回答 1查看 485关注 0票数 0

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

代码语言:javascript
复制
//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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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注释,那么缓存就可以正常工作,并得到以下输出-

代码语言:javascript
复制
DummyBean.getCity() called!
New York
New York
New York

但是如果我删除注释,那么我将得到以下输出-

代码语言:javascript
复制
DummyBean.getCity() called!
New York
DummyBean.getCity() called!
New York
DummyBean.getCity() called!
New York

这意味着缓存不起作用!

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

谢谢Nirmalya

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-25 09:15:22

在使用XML方面时,还需要添加一个aop:config部分来应用该方面。

代码语言:javascript
复制
<aop:config>
    <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.spring.example.DummyBean+.*(..))"/>
</aop:config>

假设DummyBean是由com.spring.example.DummyBeanImpl实现的接口,那么这应该可以做到这一点。这说明您希望将cacheAdvice (方面)应用于匹配表达式的bean。

也请参阅参考指南

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55333173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档