通过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 (将#修改为@)