春天如何引用另一个xml文件的bean

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

回答:

您有两种选择:

回答:

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

<bean id="yourCoolBean" class="org.jdong.MyCoolBean">

<property name="anotherBean" ref="thatOtherBean"/>

</bean>

回答:

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

以上是 春天如何引用另一个xml文件的bean 的全部内容, 来源链接: utcz.com/qa/412610.html

回到顶部