how to reference a bean of another xml file in spring

前端 未结 6 1341
花落未央
花落未央 2020-12-07 17:30

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?

相关标签:
6条回答
  • 2020-12-07 18:19

    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.

    0 讨论(0)
  • 2020-12-07 18:26

    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

    0 讨论(0)
  • 2020-12-07 18:27

    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" } );
    
    0 讨论(0)
  • 2020-12-07 18:30

    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.

    0 讨论(0)
  • 2020-12-07 18:31

    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"/>
    
    0 讨论(0)
  • 2020-12-07 18:32

    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>
    
    0 讨论(0)
提交回复
热议问题