我正在用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"/>启用了缓存,我已经提到了cachename,并且我已经用要实现缓存的方法名来配置缓存。因此,如果省略注释,就应该得到所需的输出。但为什么我得不到?
谢谢Nirmalya
发布于 2019-03-25 09:15:22
在使用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。
也请参阅参考指南
https://stackoverflow.com/questions/55333173
复制相似问题