spring-boot-ehcache 3设置xml路径(不带应用程序道具)

anhgbhbe  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(199)

我已经介绍了几个使用spring boot设置ehcache 3的示例,但它们都显示了使用application.prop文件设置xml路径:
https://howtodoinjava.com/spring-boot2/ehcache3-config-example/
https://azdanov.js.org/posts/configuring-ehcache-3-and-event-listeners-in-spring-boot/
https://www.javadevjournal.com/spring-boot/spring-boot-with-ehcache/

spring.cache.jcache.config=classpath:ehcache.xml

在Spring4(不是SpringBoot)中,可以这样做:

@Bean
public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}

@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
    EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
    cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
    cmfb.setShared(true);
    return cmfb;
}

当尝试使用它时,我得到了与此类似的错误:无法访问net.sf.ehcache.cachemanager,找不到net.sf.ehcache.cachemanager的类文件

Error:(21, 71) java: cannot access net.sf.ehcache.CacheManager
  class file for net.sf.ehcache.CacheManager not found

建议的解决方案是在application.props中设置路径
我有一个包含libs/配置的jar,它作为依赖项添加到war中。jar将在不同的war文件中使用,因此我无法在war文件中的application.props中设置路径

此解决方案可能会提供一些线索,但即使是此解决方案也涉及2.x类:

@Bean
public CacheManager cacheManager(net.sf.ehcache.CacheManager cacheManager) {
  return new EhCacheCacheManager(cacheManager);
}

@Bean
public EhCacheManagerFactoryBean cacheManagerFactory() {
  EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
  factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
  factoryBean.setShared(true);
  return factoryBean;
}

https://stackoverflow.com/a/44053913/4091828
不能使用java config设置路径吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题