1
<bean name="readerService" class="com.mayank.example1.ReaderService"/>
   <property name="reader" ref="fileReader" />
</bean>
<bean name="fileReader" class="com.mayank.example1.FileReader">
   <constructor-arg value="resources/myfile.txt" />
</bean>

Reder 服务将 reader 作为其构造函数中的参数 Reader 是接口。FileReader 是实现 Reader 的类

在春天它不带属性阅读器并抛出异常:

线程“main” org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 中的异常:来自类路径资源 [reader-beans.xml] 的 XML 文档中的第 15 行无效;嵌套异常是 org.xml.sax.SAXParseException:cvc-complex-type.2.4.a:发现以元素“属性”开头的无效内容。'{"http://www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org /schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"]}' 是预期的

4

2 回答 2

4

看起来您太早地关闭了 bean 标记(注意/>最后的,这不应该只是>吗?):

<bean name="readerService" class="com.mayank.example1.ReaderService"/>
   <property name="reader" ref="fileReader" />
</bean>
于 2013-01-10T10:10:03.480 回答
2

确保您具有所需的 xml 命名空间bean,并context在配置文件的顶部提供。我的示例使用 Spring 3.1 版本,您可能需要针对您使用的 Spring 版本进行调整。

还要注意对 readerService bean 标记的调整,该标记被过早关闭。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean name="readerService" class="com.mayank.example1.ReaderService">
   <property name="reader" ref="fileReader" />
</bean>
<bean name="fileReader" class="com.mayank.example1.FileReader">
   <constructor-arg value="resources/myfile.txt" />
</bean>

</beans>
于 2013-01-10T10:08:27.223 回答