Spring属性引用不起作用

时间:2013-01-10 10:03:24

标签: spring

<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作为其构造函数中的参数 读者是界面。 FileReader是实现Reader

的类

春天没有拿属性读者抛出异常:

线程“main”org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException中的异常:来自类路径资源[reader-beans.xml]的XML文档中的第15行无效;嵌套异常是org.xml.sax.SAXParseException:cvc-complex-type.2.4.a:找到以元素'property'开头的无效内容。其中一个'{“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“]}'预计

2 个答案:

答案 0 :(得分:4)

看起来你过早关闭bean标签(注意最后的/>,这不应该只是>吗?):

<bean name="readerService" class="com.mayank.example1.ReaderService"/>
   <property name="reader" ref="fileReader" />
</bean>

答案 1 :(得分:2)

确保配置文件顶部提供了所需的xml命名空间beancontext。我的示例使用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>