【发布时间】:2011-10-10 10:53:43
【问题描述】:
我在 xml 文件中定义了一个 Spring bean。我想从另一个 xml 文件中引用它。我该怎么办?
【问题讨论】:
标签: java spring reference javabeans
我在 xml 文件中定义了一个 Spring bean。我想从另一个 xml 文件中引用它。我该怎么办?
【问题讨论】:
标签: java spring reference javabeans
你有几个选择:
<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" } );
【讨论】:
web.xml 块抛出错误。一个param-value 似乎只允许在原地使用。
ref访问另一个xml文件中的bean而不使用<import>吗? Ref
只需使用<import resource="otherXml.xml"> 导入定义bean 的xml,您就可以使用bean 定义。
您可以在resource 属性中使用classpath::
<import resource="classpath:anotherXXML.xml" />
请参阅 Spring 参考的 this chapter 中的“3.18. 将 Bean 定义从一个文件导入另一个文件”
【讨论】:
您引用它的方式与引用同一 XML 文件中的 bean 完全相同。如果一个 spring 上下文由多个 XML 文件组成,那么所有的 bean 都是同一个上下文的一部分,因此共享一个唯一的命名空间。
【讨论】:
或者,如果您只是将 bean 重构为多个文件以防止单个 xml 文件变大,只需从其当前文件夹中引用它:
<import resource="processors/processor-beans.xml"/>
【讨论】:
您也可以通过在代码中加载多个 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。
【讨论】:
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>
【讨论】: