我正在尝试使用弹簧缓存为我的方法实现缓存。问题是在第一次之后这个方法没有被执行,这意味着属性没有被加载。如果我删除 @Cacheable 注释,它工作正常。我的要求是,只要添加新属性,该方法就应该运行并加载属性,否则它应该从缓存中返回。
@Cacheable("mycache")
public Properties loadPropertyFile() throws Throwable {
Properties properties = new Properties();
try {
logger.debug("Loading properties.");
if (this.locations != null) {
for (Resource location : this.locations) {
if (logger.isInfoEnabled()) {
logger.info("Loading properties file from " + location);
}
try {
properties = PropertiesLoaderUtils.loadAllProperties(location.getFilename());
} catch (IOException ex) {
if (this.ignoreResourceNotFound) {
if (logger.isWarnEnabled()) {
logger.warn("Could not load properties from "
+ location + ": " + ex.getMessage());
}
} else {
throw ex;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}
XML 文件:
<cache:annotation-driven />
<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>