I have a Spring b开发者_如何转开发ean defined in an xml file. I want to reference it from another xml file. How can I go about it?
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" } );
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.
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"/>
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.
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>
精彩评论