spring中如何引用另一个xml文件的bean

发布于 2024-12-09 04:07:40 字数 60 浏览 0 评论 0 原文

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

I have a Spring bean defined in an xml file. I want to reference it from another xml file. How can I go about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如痴如狂 2024-12-16 04:07:40

您有几个选项:

导入

<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" } );

You have a couple of options:

Import

<import resource="classpath:config/spring/that-other-xml-conf.xml"/>

<bean id="yourCoolBean" class="org.jdong.MyCoolBean">
    <property name="anotherBean" ref="thatOtherBean"/>
</bean>

Include in the ApplicationContext Construction

Make both files a part of your ApplicationContext when you create it => then no import is needed.

For example if you need it during testing:

@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 {...}

In case it is a web app, you'd do it in 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>

If it is a stand alone app, library, etc.. you would load your ApplicationContext as:

new ClassPathXmlApplicationContext( 
    new String[] { "classpath:META-INF/conf/spring/this-xml-conf.xml",
                   "classpath:META-INF/conf/spring/that-other-xml-conf.xml" } );
川水往事 2024-12-16 04:07:40

只需使用 导入定义 bean 的 xml,您就可以使用 bean 定义。

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

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

请参阅 Spring 参考的本章

Just import the xml defining the bean with <import resource="otherXml.xml"> and you will be able to use the bean definition.

You can use classpath: in the resource attribute:

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

See the "3.18. Importing Bean Definitions from One File Into Another" in this chapter of the Spring Reference

栀梦 2024-12-16 04:07:40

引用它就像引用同一个 XML 文件中的 bean 一样。如果 spring 上下文由多个 XML 文件组成,则所有 bean 都是同一上下文的一部分,因此共享唯一的名称空间。

You reference it exactly as you would reference a bean in the same XML file. If a spring context is composed of several XML files, all the beans are part of the same context, and thus share a unique namespace.

聊慰 2024-12-16 04:07:40

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

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

Or if you are just refactoring beans into several files to keep the single xml file from growing to large, simply reference it from its current folder:

<import resource="processors/processor-beans.xml"/>
萧瑟寒风 2024-12-16 04:07:40

您也可以通过在代码中加载多个 Spring bean 配置文件来实现此目的:

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

将所有 spring xml 文件放在项目类路径下:

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

You may also go about this by loading multiple Spring bean configuration files in the code :

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

Put all spring xml files under project classpath:

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

However, the above implementation is a lack of organizing and error prone, the better way should be organized all your Spring bean configuration files into a single XML file. For example, create a Spring-All-Module.xml file, and import the entire Spring bean files like this :

<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>

Now you can load a single xml file like this :

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

Note
In Spring3, the alternative solution is using JavaConfig @Import.

丑疤怪 2024-12-16 04:07:40

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>

Answer provided by tolitius is very detailed.
Just for the problem mentioned by Peter Butkovic

for me web.xml chunk throws error. One param-value seems to be allowed only in place. – Peter Butkovic

I met the same problem and solved by spliting two paths with "," in the same tag.

It will look like this

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                     /WEB-INF/daoContext.xml
        </param-value>
    </context-param>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文