如何解决通过XML进行Spring Ehcache 3.x配置
我正在尝试使用Spring XML上下文注入Ehcache(v3.9)javax.cache.CacheManager
(JSR-107 JCache API)。
初步尝试:
<bean id="cacheManager" factory-bean="cachingManagerProvider" factory-method="getCacheManager">
<constructor-arg name="uri" type="java.net.URI" value="classpath:/WEB-INF/ehcache.xml" />
<constructor-arg name="classLoader" type="java.lang.ClassLoader" ><null /></constructor-arg>
</bean>
<bean id="cachingManagerProvider" class="org.ehcache.jsr107.EhcacheCachingProvider" />
失败java.lang.IllegalArgumentException: Could not retrieve URI for class path resource [WEB-INF/ehcache.xml]: class path resource [WEB-INF/ehcache.xml] cannot be resolved to URL because it does not exist
我只能使用适配器类来做到这一点:
import java.io.IOException;
import javax.cache.CacheManager;
import javax.cache.spi.CachingProvider;
import org.springframework.core.io.Resource;
public class CacheManagerFactory {
private final Resource configurationLocation;
private final CachingProvider cachingProvider;
public CacheManagerFactory(CachingProvider cachingProvider,Resource configurationLocation) {
this.configurationLocation = configurationLocation;
this.cachingProvider = cachingProvider;
}
public CacheManager getCacheManager() {
try {
return this.cachingProvider.getCacheManager(this.configurationLocation.getURI(),this.getClass().getClassLoader());
} catch (IOException ex) {
throw new RuntimeException("Missing configuration",ex);
}
}
}
并在Spring上下文中:
<bean id="cacheManager" factory-bean="cacheManagerFactory" factory-method="getCacheManager" />
<bean id="cacheManagerFactory" class="com.test.CacheManagerFactory">
<constructor-arg name="cachingProvider" ref="cachingManagerProvider" />
<constructor-arg name="configurationLocation" value="/WEB-INF/ehcache.xml" />
</bean>
<bean id="cachingManagerProvider" class="org.ehcache.jsr107.EhcacheCachingProvider" />
问题是将“ /WEB-INF/ehcache.xml”指定为相对于webapp根的URI作为org.ehcache.jsr107.EhcacheCachingProvider#getCacheManager()
方法的参数。
是否可以在没有包装类的情况下进行注入?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。