如何声明父应用程序上下文

Sam*_*amo 8 spring

我发现自己在applicationContext.xml和applicationContext-test.xml中使用了两个相同的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> 
Run Code Online (Sandbox Code Playgroud)

我的测试应用程序上下文在我的单元测试中加载:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext-test.xml")
Run Code Online (Sandbox Code Playgroud)

所以,假设我在常规环境中有一个bean:

<bean name="someBean" class="com.foo.MyClass" />
Run Code Online (Sandbox Code Playgroud)

然后,在我的测试应用程序上下文中,我想引用这个bean.我该怎么做?

更新

根据skaffman的建议,我已将bean移动到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)
Run Code Online (Sandbox Code Playgroud)

我无法确定我做错了什么.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>
Run Code Online (Sandbox Code Playgroud)

ska*_*man 5

对于父上下文而言,这并不是一个特别好的用例,它主要用于设置层次结构(例如,一个根we​​bapp上下文,多个子servlet上下文).

对于您的情况,如果您只是将公共bean定义提取到单独的文件中,然后将<import>它们提取到需要它的每个上下文文件中,它将变得更简单,更容易理解.你可以用父子语境来做这件事,但是这样做会更加难以理解.

好的,举个例子,将您的共享bean定义放入一个名为的文件中shared-beans.xml,并将它(现在)放在类路径的顶层,包含:

<bean name="someBean" class="com.foo.MyClass" />
Run Code Online (Sandbox Code Playgroud)

然后,内部applicationContext-test.xml/WEB-INF/classes/applicationContext.xml,添加以下内容:

<import resource="classpath:/shared-beans.xml" />
Run Code Online (Sandbox Code Playgroud)

现在,所有bean定义都shared-beans.xml将导入到每个应用程序上下文中.通过执行此操作,您无法获得第三个应用程序上下文,只需从另一个文件导入bean定义即可.