我正在用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"/>
启用了缓存,我已经提到了缓存名称,并且已经使用要实现缓存的方法名称配置了缓存。 。因此,如果省略注释,则应该获得所需的输出。但是为什么我没有得到?
谢谢 尼尔玛利亚
答案 0 :(得分:1)
使用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