简体   繁体   English

如何使用 Spring 的 EhCache 本地 xsd

[英]How to use local xsd for EhCache with Spring

We are facing one issue about EhCache and Spring, when we point XSD file in ehcache.xml to http://ehcache.org/ehcache.xsd , everything is ok, our application can startup correct.我们面临一个关于 EhCache 和 Spring 的问题,当我们将 ehcache.xml 中的 XSD 文件指向http://ehcache.org/ehcache.xsd时,一切正常,我们的应用程序可以正确启动。 But our server can't access external website, so we changed XSD location to local as below, but the application can't startup with following exception (already copy ehcache.xsd to classes folder, same as ehcache.xml).但是我们的服务器无法访问外部网站,所以我们将 XSD 位置更改为本地,如下所示,但应用程序无法启动并出现以下异常(已将 ehcache.xsd 复制到 classes 文件夹,与 ehcache.xml 相同)。 I have googled many solution, but it still can't be fixed.我用谷歌搜索了很多解决方案,但仍然无法修复。 I need your help about this.我需要你的帮助。 Thank you very much.非常感谢你。

Spring: 3.1.0 EhCache: 2.4.2 Server: Tomcat 6.0 Java: 1.6弹簧:3.1.0 EhCache:2.4.2 服务器:Tomcat 6.0 Java:1.6

Config:配置:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="false" monitoring="autodetect" dynamicConfig="true"> 

Exception:例外:

2012-11-07 16:54:42,003 WARN [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Ignored XML validation warning>
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'ehcache.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(ErrorHandlerWrapper.java:96)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:380)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2440)
    ... more
2012-11-07 16:54:42,007 ERROR [org.springframework.web.context.ContextLoader] - <Context initialization failed>
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from ServletContext resource [/WEB-INF/classes/META-INF/ehcache.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ehcache'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    ... more
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ehcache'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)

    ...  more

Download ehcache.xsd , put it to the same folder where is your config xml file.下载ehcache.xsd ,将其放在与配置 xml 文件相同的文件夹中。 modify the xml head like below, notice the classpath prefix:像下面这样修改 xml 头,注意类路径前缀:

<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="classpath:ehcache.xsd">

This will work in the development and production environment.这将适用于开发和生产环境。

usually I set xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" , But this can't work in a offline environment.通常我会设置xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" ,但这在离线环境下是行不通的。

My Workaround is add this xsd file to absolute path, such as: c:/ehcache.xsd and point to this location in ehcache.xml.我的解决方法是将此 xsd 文件添加到绝对路径,例如:c:/ehcache.xsd 并指向 ehcache.xml 中的此位置。 Hope this will help someone who facing the same issue.希望这会帮助面临同样问题的人。

Still looking forward to find out other solution.仍然期待找到其他解决方案。

Thanks.谢谢。

In my case just downloading the file from http://ehcache.org/ehcache.xsd into src/main/resources solves the problem在我的例子中,只需将文件从http://ehcache.org/ehcache.xsd下载到src/main/resources即可解决问题

cd src/main/resources
wget http://ehcache.org/ehcache.xsd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM