需要做一个简单的弹簧3示例工作

时间:2014-07-23 17:36:47

标签: eclipse spring

我正在从一本书中学习第3弹,我在第一个例子中得到了一个例外。它在下面给出。 我的项目可以从链接 - https://github.com/double-whammy/prospring3.git

下载为zip文件

我的环境 - Eclipse STS(Eclipse for Spring)

例外:

INFO t.support.ClassPathXmlApplicationContext: 456 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@497e4b: startup date [Wed Jul 23 10:05:47 PDT 2014]; 
root of context hierarchy
INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [META-INF/spring/app-context.xml]

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 13 in XML document from class path resource [META-INF/spring/app-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 38; The prefix "p" for attribute "p:messageProvider-ref" associated with an element type "bean" is not bound.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.apress.prospring3.ch2.HelloWorldSpringDI.main(HelloWorldSpringDI.java:9)
Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 38; The prefix "p" for attribute "p:messageProvider-ref" associated with an element type "bean" is not bound.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
    ... 14 more

Spring XML Application Configuration 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:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<bean id="provider" class="com.apress.prospring3.ch2.HelloWorldMessageProvider"/> 
<bean id="renderer" class="com.apress.prospring3.ch2.StandardOutMessageRenderer" 
p:messageProvider-ref="provider"/> 
</beans>

4 个答案:

答案 0 :(得分:0)

这是因为您的xml无效:

标题有错误:xsi属性值需要双引号!

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

<击>   <bean id="renderer" class="com.apress.prospring3.ch2.StandardOutMessageRenderer" p:messageProvider-ref="provider"/>

<击> 我不知道您要配置什么,但p:messageProvider-ref作为属性是无效的xml。

<击> 我想要表达类似renderer类型StandardOutMessageRenderer的bean,它使用bean provider作为MessageProvider

<击> <bean id="provider" class="com.apress.prospring3.ch2.HelloWorldMessageProvider"/> <bean id="renderer" class="com.apress.prospring3.ch2.StandardOutMessageRenderer"><property name="messageProvider" ref="provider"/></bean>

答案 1 :(得分:0)

错误是因为xmlns:xsi的值没有引号。 更正 - xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance” 临春3书很烂。

完整的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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="provider" class="com.apress.prospring3.ch2.HelloWorldMessageProvider" />
    <bean id="renderer" class="com.apress.prospring3.ch2.StandardOutMessageRenderer"
        p:messageProvider-ref="provider" />

答案 2 :(得分:0)

你遇到的问题与maven只从所有不同的弹簧罐中挑选一个spring.handlers文件有关。

要解决此问题,请使用以下链接: http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#Merging_Content_of_Specific_Files_with_AppendingTransformer_and_XmlAppendingTransformer

请注意,您必须使用maven Assembly Plugin。

此处解释与您类似的问题: http://blog.idm.fr/2009/09/maven-assembly-plugin-and-spring-namespace-handlers.html

答案 3 :(得分:0)

您不能使用:p:messageProvider-ref="provider",它是在春季2.5

使用它:

<bean id="renderer" class="com.appress.prospring3.ch2.StandardOutMessageRenderer">      
        <property name="messageProvider" ref="provider"></property>
</bean>