Announcement Announcement Module
Collapse
No announcement yet.
Init spring across multiple modules Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Init spring across multiple modules

    Hi,

    I'm new to spring + maven and although I know a fair bit about them now, I'm getting stuck. I'm writing an application that has many modules, and depending on what functionality is required, some modules may or may not be included.

    Each one of my modules defines a spring xml configuration file full of beans, but I am unsure how to load this from the core of the app.

    My first attempt (which works) is in the maven build, pull out all spring files into a directory. Then in the code I can just load the directory. Although this works, it means the artifact I publish for each module has to be a tar.gz of file + random resources, and not just a jar file.

    Ideally I'd like the spring config in the jar file, but I am unsure how to load all config over say, 10 modules / different jars. I'd ideally have these in their seperate module jars / not combine them into an uber jar.

    Only way I can think of atm is inspecting the jars and pulling out the spring config files.

    Is there a better way of doing this in spring, or maybe theres a more "spring like" approach to the structure of my project that I do not understand / am missing.

    Some help would be greatly appreciated,
    Thanks, Jon.

  • #2
    Actually, the classpath: prefix will look in jar files so if you just reference the file names directly when creating the context list, you can keep them in the jar.

    Here's an example. Suppose the "foo" module is packaged within "foo.jar". Also in the module (for the sake of argument, let's say at the root) is a file defining all the Spring beans: "foo-beans.xml".

    Then, if you're doing this in a webapp environment (and defining a ContextLoaderListener), you could say:
    Code:
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath:foo-beans.xml
          <!-- Other bean files... -->
        </param-value>
      </context-param>
    and the file will be loaded, even though it's in a JAR.

    (The same goes if you're using a DispatcherServlet, creating an ApplicationContext by hand, etc.)

    Now things can get very interesting in one other way. Suppose each module has a Spring Bean file with the same name (let's say "module-config.xml"). You can use the classpath* prefix to load each of these in a single line, like so:
    Code:
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath*:module-config.xml
        </param-value>
      </context-param>
    I typically follow this pattern in my projects, as it provides a simple facade to the module config. Under the covers, you can always split the module-config into one or more XML files and use the <import> tag to include each within module-config.xml. For example, foo's module.config.xml might look something like this:
    Code:
    <beans ...>
        <import resource="foo-services.xml"/>
        <import resource="foo-daos.xml"/>
    </beans>
    Hope this helps
    - Don

    Comment


    • #3
      Ah that seems exactly what I need, thank you very much for your help

      Have some free time this afternoon, so should hopefully give this a go.

      Thanks again,
      Jon.

      Comment

      Working...
      X