【问题标题】:Error while setting Up Ehcahe 3 with spring boot 1.5使用 spring boot 1.5 设置 Ehcahe 3 时出错
【发布时间】:2018-04-24 19:26:00
【问题描述】:

我最近从1.4升级到spring boot 1.5.7,发现Ehcache已经从2.x升级到了3.x。配置都变了。现在我无法设置缓存配置。

我尝试按照 Ehcache 官方blog 上的步骤操作,但没有成功。

这是我的ehcache-dev.xml

<config
        xmlns='http://www.ehcache.org/v3'
        xmlns:jcache='http://www.ehcache.org/v3/jsr107'>

    <service>
        <jcache:defaults>

            <jcache:cache name="categoryCache" template="catalog-cache"/>
            <jcache:cache name="cfCategoryCache" template="catalog-cache"/>

            <jcache:cache name="ebooks" template="other-cache"/>
            <jcache:cache name="userNameToken" template="other-cache"/>

        </jcache:defaults>
    </service>

    <cache-template name="catalog-cache">
        <heap unit="entries">200</heap>
        <expiry>
            <ttl unit="minutes">10</ttl>
        </expiry>
    </cache-template>

    <cache-template name="other-cache">
        <!--<listeners>-->
        <!--<listener>-->
        <!--<class>org.terracotta.ehcache.EventLogger</class>-->
        <!--<event-firing-mode>ASYNCHRONOUS</event-firing-mode>-->
        <!--<event-ordering-mode>UNORDERED</event-ordering-mode>-->
        <!--<events-to-fire-on>CREATED</events-to-fire-on>-->
        <!--<events-to-fire-on>UPDATED</events-to-fire-on>-->
        <!--<events-to-fire-on>EXPIRED</events-to-fire-on>-->
        <!--<events-to-fire-on>REMOVED</events-to-fire-on>-->
        <!--<events-to-fire-on>EVICTED</events-to-fire-on>-->
        <!--</listener>-->
        <!--</listeners>-->
        <heap unit="entries">1000</heap>
        <expiry>
            <ttl unit="minutes">15</ttl>
        </expiry>
    </cache-template>
</config>

我的application-dev.properties 文件包含以下行:

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

dev 配置文件上运行时,会产生错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception; nested exception is javax.cache.CacheException: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/**********/target/********/WEB-INF/classes/ehcache-dev.xml
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 135 more
Caused by: javax.cache.CacheException: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/**********/target/********/WEB-INF/classes/ehcache-dev.xml
    at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:327)
    at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:128)
    at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:79)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.createCacheManager(JCacheCacheConfiguration.java:115)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.jCacheCacheManager(JCacheCacheConfiguration.java:97)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$46f8ca07.CGLIB$jCacheCacheManager$1(<generated>)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$46f8ca07$$FastClassBySpringCGLIB$$fee1b5ae.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$46f8ca07.jCacheCacheManager(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 136 more
Caused by: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/**********/target/********/WEB-INF/classes/ehcache-dev.xml
    at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:167)
    at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:131)
    at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:324)
    ... 150 more
Caused by: org.xml.sax.SAXParseException; systemId: file:/**********/target/********/WEB-INF/classes/ehcache-dev.xml; lineNumber: 20; columnNumber: 17; cvc-complex-type.2.4.a: Invalid content was found starting with element 'expiry'. One of '{"http://www.ehcache.org/v3":heap-store-settings, "http://www.ehcache.org/v3":disk-store-settings, "http://www.ehcache.org/v3":service-configuration}' is expected.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
    at org.ehcache.xml.ConfigurationParser.<init>(ConfigurationParser.java:165)
    at org.ehcache.xml.XmlConfiguration.parseConfiguration(XmlConfiguration.java:175)
    at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:163)
    ... 152 more

现在,如果我没记错的话,在 cache-template 元素中接受 expiry 元素是有问题的。但事实并非如此,因为 here 明确提到 A &lt;cache-template&gt; element may contain all the same child elements as a &lt;cache&gt; element.cache 元素确实有一个 expiry 元素。

我无法理解我在这里缺少什么。任何帮助表示赞赏。 TIA

【问题讨论】:

    标签: caching spring-boot spring-4 jcache ehcache-3


    【解决方案1】:

    您可以查看 ehcache 文档中给出的版本 3(jsr107 扩展)的 XML 示例,请参阅 link。查看您的 XML 格式是否遵循那里给出的标签。

    【讨论】:

      【解决方案2】:

      显然,问题出在元素的顺序上。 heap 元素必须在 expiry 元素之后。

      下面的配置没问题:

       <cache-template name="catalog-cache">        
          <expiry>
              <ttl unit="minutes">10</ttl>
          </expiry>
          <heap unit="entries">200</heap>
      </cache-template>
      

      这是没有意义的,因为顺序无关紧要。已向 EhCache 提交了bug-report

      【讨论】: