Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何声明父应用程序上下文_Spring - Fatal编程技术网

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

Spring 如何声明父应用程序上下文,spring,Spring,我发现自己在applicationContext.xml和applicationContext-test.xml中使用了两个相同的bean。我希望我的测试上下文能够从我的应用程序上下文继承,以避免重复我自己 我已经看到很多材料表明可以声明父应用程序上下文和该上下文中的引用bean,但我找不到有用的示例。有人能帮忙吗 更新 作为一些背景信息,我的普通应用程序上下文正在web.xml中加载: <context-param> <description>Applicati

我发现自己在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> 
假设在我的常规上下文中有一个bean:

<bean name="someBean" class="com.foo.MyClass" />
我不能确定我做错了什么。这个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>

${module.name}
${businessUnit}
${product}
${env}
${machineName}
${collectionSet.company}
${route.tag}
${timeout}

对于父上下文来说,这并不是一个特别好的用例,它主要用于设置层次结构(例如,一个根webapp上下文、多个子servlet上下文)

对于您的情况,如果您只需将公共bean定义提取到一个单独的文件中,然后
将它们提取到每个需要它的上下文文件中,就会变得更简单、更容易理解。您可以在父子上下文中执行此操作,但不必要的是,这将更难理解

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

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

sharedbean.xml
中的所有bean定义现在都将导入到每个应用程序上下文中。这样做不会获得第三个应用程序上下文,只需从另一个文件导入bean定义。

您可以将公共声明移动到单独的XML文件中,并将其放置在类路径中(以便从测试中轻松访问)。然后您可以执行以下操作:

<import resource="classpath:/shared-beans.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> 

您还可以使用skaffman建议的
,从两个上下文中包括
common beans.xml

这取决于您的设置方式。请举一个你使用上下文的例子。@skaffman:问题已更新。感谢您的示例。您仍然需要
顶级元素……您能澄清一下吗?当你说“一个单独的文件”时,这是一个豆子工厂吗?如果没有,是什么?你说我可以从这个文件中导入它们,但是你能提供一个例子来演示如何做到这一点吗?我得到了一个SAXParser异常。请参阅更新的问题。不确定我可能做错了什么。
<import resource="classpath:/shared-beans.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"})