2010-11-30 3 views
8

自分自身が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> 
+1

これは設定方法によって異なります。あなたのコンテキストを使用する例を挙げてください。 – skaffman

+0

@skaffman:質問が更新されました。例が分かりました。 – Samo

+2

あなたはまだ ''トップレベルの要素が必要です.... – skaffman

答えて

5

これは、例えば(階層を設定することが主に有用である、親コンテキストのために特に良いユースケースとしてつのルートを私に当たりませんWebアプリケーションコンテキスト、複数の子サーブレットコンテキスト)。

一般的なBean定義を個別のファイルに抽出してから、それを必要とする各コンテキストファイルに<import>として抽出するだけで簡単に理解できます。 になりますが、これは親子コンテキストで行いますが、不必要に理解するのが難しくなります。

OK、そう例では、ファイルに共有Bean定義を置くshared-beans.xmlと呼ばれ、クラスパスのトップレベルで(今のところ)に入れて、含む:

<bean name="someBean" class="com.foo.MyClass" /> 

を次に、applicationContext-test.xml内部そして、/WEB-INF/classes/applicationContext.xmlは、以下を追加します。

<import resource="classpath:/shared-beans.xml" /> 

shared-beans.xmlでBean定義のすべてが今、各アプリケーションのコンテキストにインポートされます。これを行うことで3番目のアプリケーションコンテキストを取得することはできません。別のファイルからBean定義をインポートするだけです。

+1

お願いしますか? 「別ファイル」と言うと、これは豆工場ですか?そうでない場合、それは何ですか?あなたは私がこのファイルからそれらをインポートすることができますが、これがどのように行われたかを示す例を提供できますか? – Samo

+0

SAXParserの例外が発生します。更新された質問をご覧ください。私は何が間違っているかもしれないか分からない。 – Samo

0

共通の宣言を別々のXMLファイルに移動し、クラスパスに配置することができます(テストからのアクセスが容易です)。次に、次の操作を実行できます。

<context-param> 
    <description>Application Contexts for Spring</description> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/classes/applicationContext.xml 
     classpath:/common-beans.xml 
    </param-value> 
</context-param> 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(
    locations = {"/applicationContext-test.xml", "common-beans.xml"}) 

skaffmanにより示唆されるようにまた、<import>を使用して両方のコンテキストからcommon-beans.xmlを含めることができます。

関連する問題