45

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?

Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
Jeffrey.W.Dong
  • 1,057
  • 3
  • 14
  • 21

6 Answers6

59

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" } );
tolitius
  • 22,149
  • 6
  • 70
  • 81
  • syntax issue? @ContextConfiguration(locations={"base-context.xml"}) – Kalpesh Soni Aug 27 '13 at 17:21
  • 4
    for me `web.xml` chunk throws error. One `param-value` seems to be allowed only in place. – Peter Butkovic Apr 18 '14 at 05:51
  • For import: Can't we access beans in another xml file using `ref` without using `` if the xml is defined in the same application? [Ref](https://mkyong.com/spring/spring-bean-reference-example/#:~:text=If%20you%20are%20referring%20to,tag%2C%20'%20bean%20'%20attribute.&text=In%20this%20example%2C%20the%20bean,beans%20in%20'%20Spring%2DOutput.) – laahaa Dec 26 '21 at 02:48
7

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

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • I did that, but after refresh, system fell into a infinite status. did i do anything wrong? – Jeffrey.W.Dong Oct 10 '11 at 11:04
  • Didn't spring trace any exception, or information on what it was doing in that infinite status? Also, take a look at @JBNizet 's suggestion, this would be only necessary if those xml's are not part of the same application context. See the "3.2.2.1. Composing XML-based configuration metadata" section in [this chapter](http://static.springsource.org/spring/docs/2.5.x/reference/beans.html) of the 2.5 reference (the link I gave in my answer may be of a somewhat outdated spring version). – Xavi López Oct 10 '11 at 11:14
5

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

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"/>
Tom Chamberlain
  • 2,955
  • 20
  • 24
1

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.

Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
0

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>