【问题标题】:how to reference a bean of another xml file in spring如何在spring中引用另一个xml文件的bean
【发布时间】:2011-10-10 10:53:43
【问题描述】:

我在 xml 文件中定义了一个 Spring bean。我想从另一个 xml 文件中引用它。我该怎么办?

【问题讨论】:

    标签: java spring reference javabeans


    【解决方案1】:

    你有几个选择:

    导入

    <import resource="classpath:config/spring/that-other-xml-conf.xml"/>
    
    <bean id="yourCoolBean" class="org.jdong.MyCoolBean">
        <property name="anotherBean" ref="thatOtherBean"/>
    </bean>
    


    包含在ApplicationContext 构造中

    在创建 ApplicationContext 时让这两个文件成为您的一部分 => 然后不需要导入。

    例如,如果您在测试期间需要它:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "classpath:META-INF/conf/spring/this-xml-conf.xml",
                        "classpath:META-INF/conf/spring/that-other-xml-conf.xml" })
    public class CleverMoneyMakingBusinessServiceIntegrationTest {...}
    

    如果它是一个网络应用,你可以在web.xml

    <context-param> 
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/conf/spring/this-xml-conf.xml</param-value>
        <param-value>WEB-INF/conf/spring/that-other-xml-conf.xml</param-value>
    </context-param>
    
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    如果它是一个独立的应用程序、库等。您可以将ApplicationContext 加载为:

    new ClassPathXmlApplicationContext( 
        new String[] { "classpath:META-INF/conf/spring/this-xml-conf.xml",
                       "classpath:META-INF/conf/spring/that-other-xml-conf.xml" } );
    

    【讨论】:

    • 语法问题? @ContextConfiguration(locations={"base-context.xml"})
    • 对我来说 web.xml 块抛出错误。一个param-value 似乎只允许在原地使用。
    • 对于导入:如果xml是在同一个应用程序中定义的,我们不能使用ref访问另一个xml文件中的bean而不使用&lt;import&gt;吗? Ref
    【解决方案2】:

    只需使用&lt;import resource="otherXml.xml"&gt; 导入定义bean 的xml,您就可以使用bean 定义。

    您可以在resource 属性中使用classpath:

    <import resource="classpath:anotherXXML.xml" />
    

    请参阅 Spring 参考的 this chapter 中的“3.18. 将 Bean 定义从一个文件导入另一个文件”

    【讨论】:

    • 我这样做了,但是刷新后,系统陷入了无限状态。我做错什么了吗?
    • 没有 spring 跟踪任何异常,或者它在无限状态下正在做什么的信息?另外,看看@JBNizet 的建议,只有当这些 xml 不是同一应用程序上下文的一部分时才需要这样做。请参阅 2.5 参考的this chapter 中的“3.2.2.1. 编写基于 XML 的配置元数据”部分(我在回答中给出的链接可能是有点过时的 spring 版本)。
    【解决方案3】:

    您引用它的方式与引用同一 XML 文件中的 bean 完全相同。如果一个 spring 上下文由多个 XML 文件组成,那么所有的 bean 都是同一个上下文的一部分,因此共享一个唯一的命名空间。

    【讨论】:

      【解决方案4】:

      或者,如果您只是将 bean 重构为多个文件以防止单个 xml 文件变大,只需从其当前文件夹中引用它:

      <import resource="processors/processor-beans.xml"/>
      

      【讨论】:

        【解决方案5】:

        您也可以通过在代码中加载多个 Spring bean 配置文件来解决此问题:

        ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] {"Spring-Common.xml", "Spring-Connection.xml","Spring-ModuleA.xml"});
        

        将所有spring xml文件放到项目classpath下:

        project-classpath/Spring-Common.xml
        project-classpath/Spring-Connection.xml
        project-classpath/Spring-ModuleA.xml
        

        然而,上面的实现缺乏组织并且容易出错,更好的方法应该是把你所有的 Spring bean 配置文件组织到一个单独的 XML 文件中。例如,创建一个Spring-All-Module.xml 文件,然后像这样导入整个 Spring bean 文件:

        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        
            <import resource="common/Spring-Common.xml"/>
            <import resource="connection/Spring-Connection.xml"/>
            <import resource="moduleA/Spring-ModuleA.xml"/>
        
        </beans>
        

        现在您可以像这样加载单个 xml 文件:

        ApplicationContext context = 
            new ClassPathXmlApplicationContext(Spring-All-Module.xml);
        

        注意 在 Spring3 中,替代解决方案是使用JavaConfig @Import

        【讨论】:

          【解决方案6】:

          tolitius提供的答案很详细。 只是针对Peter Butkovic提到的问题

          对我来说 web.xml 块会引发错误。一个参数值似乎只允许到位。 ——彼得·布特科维奇

          我遇到了同样的问题,通过在同一个标​​签中用“,”分割两条路径来解决。

          看起来像这样

              <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>/WEB-INF/applicationContext.xml,
                               /WEB-INF/daoContext.xml
                  </param-value>
              </context-param>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-05
            • 2015-10-23
            • 2015-10-08
            • 2021-03-08
            • 2020-01-07
            • 1970-01-01
            相关资源
            最近更新 更多