我在使用OSGi环境中的Spring Cache时遇到了问题。也许你可以告诉我我错过了什么。
我已成功配置Spring Cache以在
等测试期间工作@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-test.xml"})
public class CacheDictTest {
@Autowired
Dictionary dictionary;
@Test
public void getDict5Times() {
for (int i = 0; i < 5; i++) {
System.out.println(dictionary.getSourceDomains());
}
Assert.assertTrue(true);
}
}
选择执行一次,然后我有5个漂亮的打印。
但是我无法将其打包成
Cacheable注释似乎被忽略了。每次调用dictionary.getSourceDomains()时都会执行查询。 我使用ServiceMix 5.3.0作为容器。
我的配置:
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="dictionary"/>
</set>
</property>
</bean>
词典:
public class DictionaryImpl implements Dictionary {
private DictionaryDao repository;
public DictionaryDao getRepository() {
return repository;
}
public void setRepository(DictionaryDao repository) {
this.repository = repository;
}
@Override
public List<String> getSourceDomains() {
List<DictEntry> entries = repository.getDictionary(DictTypeEnum.SOURCE_DOMAIN);
List<String> domains = new ArrayList<>();
for(DictEntry entry : entries) {
domains.add(entry.getKey());
}
return domains;
}
}
和dao
public class DictionaryDaoImpl extends BaseDaoImpl implements DictionaryDao {
private static final Logger LOG = LoggerFactory.getLogger(DictionaryDaoImpl.class);
@Override
@Cacheable(value="dictionary", key="#type")
public List<DictEntry> getDictionary(DictTypeEnum type) {
LOG.info("Loading {}", type);
Query q = getSession().createQuery("from DictEntry where type=:type");
q.setParameter("type", new DictType(type.getTypeId()));
List results = q.list();
LOG.debug("Results {}", results);
return results;
}
}
我尝试了什么
答案 0 :(得分:0)
问题是缺少在osgi清单中导入Cacheable注释包。
org.springframework.cache.annotation
Servicemix没有显示缺少类的任何错误,只是让服务忽略Cacheable注释。