我试图设置一个简单的弹簧应用程序,我得到以下异常.这是在日食靛蓝独立运行.
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>
解决方法
你确定你在类路径上有spring bean吗?
这个错误通常意味着它找不到一个spring.schemas(在spring-beans.jar中)来解释这个命名空间是什么意思.
其他选项是Maven Shade插件已经损坏了spring.schemas,但是不太可能,因为你没有提到Maven.