how to reference a bean of another xml file in spring

前端 未结 6 1355
花落未央
花落未央 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:27

    You have a couple of options:

    Import

    
    
    
        
    
    


    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:

     
        contextConfigLocation
        WEB-INF/conf/spring/this-xml-conf.xml
        WEB-INF/conf/spring/that-other-xml-conf.xml
    
    
     
        org.springframework.web.context.ContextLoaderListener
    
    

    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" } );
    

提交回复
热议问题