Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Java 如何从XML中指定父应用程序上下文?_Java_Spring - Fatal编程技术网

Java 如何从XML中指定父应用程序上下文?

Java 如何从XML中指定父应用程序上下文?,java,spring,Java,Spring,我需要在springs中将已经存在的上下文重新用作父上下文 我有两个xml文件-bean.xml(父上下文),child-bean.xml(子上下文) 我能行 ApplicationContext context2 = new ClassPathXmlApplicationContext(new String[] { "child-bean.xml" }, context); 但这种方法是通过代码实现的。 有没有办法通过XML来控制这一点 我试着把它放进child bean.xml <i

我需要在springs中将已经存在的上下文重新用作父上下文

我有两个xml文件-bean.xml(父上下文),child-bean.xml(子上下文)

我能行

ApplicationContext context2 = new ClassPathXmlApplicationContext(new String[] { "child-bean.xml" }, context);
但这种方法是通过代码实现的。 有没有办法通过XML来控制这一点

我试着把它放进child bean.xml

<import resource="bean.xml"/>

但这不一样,因为看起来父级中的所有bean都在重新创建

TL;博士


我需要确保在创建子上下文时不会再次创建父上下文中bean的新实例。在不改变应用程序上下文初始化方式的情况下,是否可以通过XML实现这一点

在web.xml中,您可以使用以下方法加载多个应用程序上下文:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:bean1.xml,
        classpath*:bean2.xml
    </param-value>
</context-param>

上下文配置位置
类路径*:bean1.xml,
classpath*:bean2.xml

在我的例子中,我使用ref parent

我希望我的xml配置对您有所帮助

<!--parent -->
<bean id="abstractService" abstract="true"
        class="com.lizjason.spring.AbstractService">
        <property name="companyName"
            value="lizjason"/>
    </bean>

    <bean id="shippingService"
        parent="abstractService"
        class="com.lizjason.spring.ShippingService">
        <property name="shippedBy" value="lizjason"/>
    </bean>


我这里说的不是父bean,而是父应用程序上下文我没有web.xml,它是一个独立的java应用程序。
public class WebAppInitializer implements WebApplicationInitializer{

    @Override
     public void onStartup(ServletContext container) throws ServletException{
        XmlWEbApplicationContext applicationContext = 
       new   XmlWebApplicationContext();
       applicationContext.setConfigLocation("classpath:spring/*.xml");
       container.addListener(new ContextLoaderListener(applicationContext));
     }
}