在Spring项目中配置缓存时出现异常:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from file [D:\workspaces\eclipse_svn\NewsPortalProject\WebContent\WEB-INF\classes\config\ehcache.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ehcache'.
是在配置文件中省略命名空间语句导致的~
原配置文件
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="false"/>
<cache name="newslist"
eternal="false"
timeToIdleSeconds="360"
timeToLiveSeconds="3600"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU" />
</ehcache>添加命名空间之后的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="false"/>
<cache name="newslist"
eternal="false"
timeToIdleSeconds="360"
timeToLiveSeconds="3600"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU" />
</ehcache>解释:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"中xsi的意思是 :
本xml文件中要用到某些来自xsi代表的“http://www.w3.org/2001/XMLSchema-instance”这个命名空间的元素
比如用来引入无命名空间schema文件的noNamespaceSchemaLocation="XXX";
以及引入自带命名空间的schema文件的schemaLocation="XXX"这些元素。
这些元素是包含在xsi命名空间中的,所有的xml文件只要引用这些元素 就要引入xsi这个命名空间。
xsi这三个字母不是硬性规定,只是大家都这么用,方便阅读而已。

本文介绍了一种在Spring项目中配置ehcache时遇到的异常情况,并提供了详细的解决方案。通过对比正确的配置文件与错误配置之间的差异,特别是关于命名空间声明的重要性。

1万+

被折叠的 条评论
为什么被折叠?



