how to reference a bean of another xml file in spr

2019-01-16 10:08发布

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?

5条回答
小情绪 Triste *
2楼-- · 2019-01-16 10:31

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.

查看更多
Evening l夕情丶
3楼-- · 2019-01-16 10:42

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" } );
查看更多
闹够了就滚
4楼-- · 2019-01-16 10:46

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"/>
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-16 10:47

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

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-16 10:50

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.

查看更多
登录 后发表回答