Spring キャッシュを使用してメソッドにキャッシングを実装しようとしています。問題は、このメソッドが初めて実行されないことです。つまり、プロパティが読み込まれていません。@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>