【问题标题】:Spring Abstraction Cache in OSGi EnvironmentOSGi 环境中的 Spring 抽象缓存
【发布时间】:2015-03-15 00:12:30
【问题描述】:

我在使 Spring Cache 在 OSGi 环境中工作时遇到问题。也许你可以告诉我我缺少什么。

我已成功配置 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;
  }



}

和道

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;
  }


}

我尝试了什么

  • 将 @Cacheable 注释移动到 DictionaryDao(接口)、DictionaryImpl 或 Dictionary(接口) - 无效。
  • 使用不同的缓存实现(ehcache 代替 JDK ConcurrentMap-based Cache)- 无效

【问题讨论】:

    标签: caching osgi bundle spring-cache apache-servicemix


    【解决方案1】:

    问题是osgi清单中缺少可缓存注释包的导入。

    org.springframework.cache.annotation
    

    Servicemix 没有显示任何缺少类的错误,只是让服务工作忽略 Cacheable 注释。

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 2017-04-20
      • 2012-05-23
      • 2012-10-16
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多