How to use local xsd for EhCache using Spring

We faced one problem about EhCache and Spring, when we point the XSD file in ehcache.xml to http://ehcache.org/ehcache.xsd , everything is fine, our application can start correctly. But our server cannot access the external website, so we changed the XSD location to local, as shown below, but the application cannot start with the following exception (already copy ehcache.xsd to the class folder, as well as ehcache.xml) . I have many solutions to solve the problem, but it is still impossible to fix. I need your help. Thank you very much.

Spring: 3.1.0 EhCache: 2.4.2 Server: 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"> 

An 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 
+6
source share
3 answers

Download ehcache.xsd , put it in the same folder where your xml configuration file is located. change the xml chapter as shown below, pay attention to the classpath prefix:

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

This will work in a development and production environment.

+9
source

I usually set xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" , but this cannot work in a standalone environment.

+7
source

My workaround is adding this xsd file to the absolute path, for example: c: /ehcache.xsd, and point to that place in ehcache.xml. Hope this helps someone who is facing the same problem.

Still looking forward to finding another solution.

Thanks.

+1
source

All Articles