自分自身がapplicationContext.xmlとapplicationContext-test.xmlに2つの同一のBeanを使用しています。自分のテストコンテキストを自分のアプリコンテキストから継承し、自分自身を繰り返さないようにしたい。親アプリケーションのコンテキストを宣言する方法
親アプリケーションのコンテキストとそのコンテキストからの参照Beanを宣言できますが、役に立つ例は見つけられません。誰も助けることができますか?いくつかの背景情報として
更新 は、私の通常のアプリケーションコンテキストは、web.xmlにロードされている:
<context-param>
<description>Application Contexts for Spring</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
私のテストアプリケーションコンテキストは、私のユニットテストにロードされます。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext-test.xml")
だから私は普通のコンテキストでBeanを持っているとしましょう:
<bean name="someBean" class="com.foo.MyClass" />
次に、私のテストアプリケーションのコンテキストで、このBeanを参照したいと思います。どうすればいいのですか? skaffmanの提案パー
更新
、私はSharedBeans.xmlファイルに豆を移動し、私のapplicationContext.xmlをにインポートしました。ただし、これによってSAXParser例外が発生します。
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:SharedBeans.xml]
Offending resource: ServletContext resource [/WEB-INF/classes/applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from class path resource [SharedBeans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'bean'.
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
私は間違っているとは確信できません。 Beanはコンテキストファイルでうまく動作していましたが、私が行った作業はすべて切り取って新しいファイルに貼り付けました。ここでは、その全体がSharedBeans.xmlの内容は以下のとおりです。
<bean name="properties" class="com.foo.Properties">
<constructor-arg><value>${module.name}</value></constructor-arg>
<constructor-arg><value>${businessUnit}</value></constructor-arg>
<constructor-arg><value>${product}</value></constructor-arg>
<constructor-arg><value>${env}</value></constructor-arg>
<constructor-arg><value>${machineName}</value></constructor-arg>
<constructor-arg><value>${collectionSet.company}</value></constructor-arg>
<constructor-arg><value>${route.tag}</value></constructor-arg>
<constructor-arg><value>${timeout}</value></constructor-arg>
</bean>
これは設定方法によって異なります。あなたのコンテキストを使用する例を挙げてください。 – skaffman
@skaffman:質問が更新されました。例が分かりました。 – Samo
あなたはまだ ''トップレベルの要素が必要です.... –
skaffman