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 は Interface です。FileReader は Reader を実装するクラスです

春には、プロパティリーダーを取得せず、例外をスローします:

スレッド「メイン」での例外 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"]}' が期待される

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のバージョンに合わせて調整する必要がある場合があります。

また、早すぎて閉じられていたreaderServiceBeanタグの調整にも注意してください。

<?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 に答える