how to reference a bean of another xml file in 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
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" } );