Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reference a bean of another xml file in spring

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?

like image 973
Jeffrey.W.Dong Avatar asked Oct 10 '11 10:10

Jeffrey.W.Dong


People also ask

How do I reference a bean in XML?

If you are referring to a bean in different XML file, you can reference it with a ' ref ' tag, ' bean ' attribute. In this example, the bean “OutputHelper” declared in ' Spring-Common. xml ' can access to other beans in ' Spring-Output.

What is ref bean in Spring?

In Spring we need to use <ref> element to inform spring container about the object dependency. In Spring, beans can "access" to each other by specify the bean references in the same or different bean configuration file.In spring we can write multiple configuration xml file.

What is bean tag in XML?

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container. For example, in the form of XML <bean/> definitions which you have already seen in the previous chapters.

Where do I put bean XML?

For a web application, the beans. xml file must be in the WEB-INF directory. For EJB modules or JAR files, the beans. xml file must be in the META-INF directory.


3 Answers

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" } ); 
like image 177
tolitius Avatar answered Sep 22 '22 14:09

tolitius


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

like image 40
Xavi López Avatar answered Sep 20 '22 14:09

Xavi López


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.

like image 40
JB Nizet Avatar answered Sep 21 '22 14:09

JB Nizet