【发布时间】:2019-03-25 07:42:09
【问题描述】:
我正在使用 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"/> 启用了缓存,我已经提到了缓存名称,并且我已经使用要实现缓存的方法名称配置了缓存。因此,如果我省略注释,我应该得到所需的输出。但是为什么我没有得到呢?
谢谢 尼玛利亚
【问题讨论】:
标签: java spring spring-cache