![](/img/trans.png)
[英]Getting an error in spring application: cvc-elt.1: Cannot find the declaration of element 'beans'
[英]Getting error message with spring “cvc-elt.1: Cannot find the declaration of element 'beans'.”
我正在嘗試設置一個簡單的spring應用程序,我得到以下異常。 這是在eclipse indigo中獨立運行的。
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Line 2 in XML document from class path resource [context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
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)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
這是我的代碼的初始部分:
public static void main(String[] args) {
try {
BeanFactory beanfactory = new ClassPathXmlApplicationContext(
"context.xml");
FirstBean bean = (FirstBean) beanfactory.getBean("show");
這是我的context.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="anotherBean" class="AnotherBean" />
<bean id="show" class="FirstBean">
<constructor-arg ref="anotherBean" />
</bean>
<bean id="populateFD" class="PopulateFactData">
<constructor-arg value="localhost" />
<constructor-arg value="3309" />
</bean>
</beans>
你確定你在classpath上有spring-beans
嗎?
此錯誤通常意味着它找不到spring.schemas
(在spring-beans.jar
)向它解釋該命名空間的含義。
其他選項是Maven Shade插件損壞了spring.schemas
,但由於你沒有提到Maven,所以情況不太可能。
maven shade插件似乎取代了jar中的spring.schemas文件,因此使用每個jar中的所有單獨的spring.schema內容創建我們自己的一個應該可以解決問題。
當我在STS中遇到這個問題時,我剛剛清理了項目並且它有效。
可能存在Spring Bean版本和xsd定義不匹配的可能性。
例如bean xsi:schemaLocation =“http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework。 org / schema / mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema /beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http:// www。 springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema /context/spring-context-3.0.xsd
而春季版則是3.0.5-Release
檢查類路徑版本並保持相同。 建議立即進行刪除
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.