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?
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.
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.
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.
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.
You have a couple of options:
<import resource="classpath:config/spring/that-other-xml-conf.xml"/> <bean id="yourCoolBean" class="org.jdong.MyCoolBean"> <property name="anotherBean" ref="thatOtherBean"/> </bean>
ApplicationContext
ConstructionMake 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" } );
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With