从不同目录中不同xml文件中定义的其他bean读取1个bean

时间:2012-06-25 09:16:40

标签: spring java-ee persistence

我们如何从不同目录中不同xml文件中定义的其他bean中读取一个bean。例如,如果在

中定义了bean

src \ main \ resources \ serviceconfig \ org \ project \ core_spring \ file1.xml

需要引用其中定义的另一个bean SRC \主\的java \有机\项目\芯\公地\ persistence_spring \ file2.xml

有没有办法引用它或将file2.xml的资源导入file1.xml?

3 个答案:

答案 0 :(得分:1)

中指定整个相对类路径
<import resource="\org\project\core\commons\persistence_spring\file2.xml" />

为我解决了这个问题。

尽管还没有尝试过赵的答案。如果那个是有效的,因为进入相对路径并且每次我需要引用它时导入一些资源都会非常麻烦。

答案 1 :(得分:0)

您可以将file2.xml导入file1.xml。使用

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

导入后,您可以使用file2.xmlfile1.xml中定义的bean。

答案 2 :(得分:0)

您可以使用web.xml中的<context-param>标记在应用程序中包含多个bean定义文件。您可以在类路径或Web文件夹中引用这两个上下文。并且您将能够使用这些文件中定义的bean,就好像它们是在一个文件中定义的一样。

例如:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/com/your-company/file1.xml
        /WEB-INF/file2.xml
    </param-value>
</context-param>