Can I compose a Spring Configuration File from smaller ones?

前端 未结 7 1903
后悔当初
后悔当初 2021-02-13 02:38

I have a handful of projects that all use one project for the data model. Each of these projects has its own applicationContext.xml file with a bunch of repetitive data stuff w

相关标签:
7条回答
  • 2021-02-13 03:08

    Another thing to note is that although you can do this, if you aren't a big fan of XML you can do a lot of stuff in Spring 2.5 with annotations.

    0 讨论(0)
  • 2021-02-13 03:08

    Yes, you can using the tag inside the "Master" bean file. But what about the why? Why not listing the files in the contextConfigLocation context param of the wab.xml or als locations array of the bean factory?

    I think mutliple files are much easier to handle. You may choose only some of them for a test, simply add rename or remove a part of the application and you may boundle different applications with the same config files (a webapp and a commandline version with some overlapping bean definitions).

    0 讨论(0)
  • 2021-02-13 03:13

    We do this in our projects at work, using the classpath* resource loader in Spring. For a certain app, all appcontext files containing the application id will be loaded:

    classpath*:springconfig/spring-appname-*.xml
    
    0 讨论(0)
  • 2021-02-13 03:13

    Here's what I've done for one of my projects. In your web.xml file, you can define the Spring bean files you want your application to use:

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          /WEB-INF/applicationContext.xml
          /WEB-INF/modelContext.xml
          /WEB-INF/ui.xml
        </param-value>
      </context-param>
    

    If this isn't defined in your web.xml, it automatically looks for /WEB-INF/applicationContext.xml

    0 讨论(0)
  • 2021-02-13 03:21

    Given what Nicholas pointed me to I found this in the docs. It allows me to pick at runtime the bean contexts I'm interested in.

    GenericApplicationContext ctx = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
    xmlReader.loadBeanDefinitions(new ClassPathResource("modelContext.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("uiContext.xml"));
    ctx.refresh();
    
    0 讨论(0)
  • 2021-02-13 03:22

    Yes, you can do this via the import element.

    <import resource="services.xml"/>
    

    Each element's resource attribute is a valid path (e.g. classpath:foo.xml)

    0 讨论(0)
提交回复
热议问题