Ittai
Ittai

Reputation: 5915

How to arrange spring context configuration files so they match the project dependencies?

I have a web-application which is dependent on two other modules.
For simplicity let's call them ServiceA module and ServiceB module.
Each of these modules has various different dependencies and also a common dependency on the Entities module.
Each of the above mentioned modules declares its own spring context file with information pertaining to its scope.
I'm trying now to decide how to "wire" these configuration files between the projects and I'm a bit stumped.

I know one option is just to declare all the "end" files (i.e. ServiceA, ServiceB and Entities) in the web.xml of the web-app (in the contextConfigLocation param) but I don't like that option particularly since my actual use-case is more complex and has more internal dependencies which are shared.

My original intent was to declare in the contextConfigLocation param only the config files for ServiceA and ServiceB since these are the only projects which the web-app is directly dependent upon (this is easy to see by looking at the maven pom), and then have both ServiceA and ServiceB to include this directive in their spring context configuration file <import resource="classpath:EntitiesContext.xml">. The advantage with this approach is that it is consistent with the maven transitive approach where I declare what I'm dependent upon and if that module is dependent upon something it will drag it along with it. The problem with this approach is that I read here that all the beans in the Entities module will be created twice (although only one instance will remain at the end) which is an expensive and unneeded action.

I'd like very much to hear how people solve this use-case since I don't think I've hit any corner case.

Thanks

Update The syntax I ended up using was classpath*:META-INF/*/*Context.xml since there are some issues with the syntax Thomasz suggested.
For additional reading see the bug report of spring (which partially resolved the issue) and a blog post regarding the issue

Upvotes: 3

Views: 2584

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328750

Have you considered using an annotation based configuration?

That should allow you to reduce the size of your XML config to just a few lines.

Alternatively, using top-level Java based config would allow you to pull in the parts that you want in a controlled way (without having to search the whole classpath) and without any duplicate beans or any XML files.

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340923

What about following some naming convention of config files and simply picking up all that are available on the CLASSPATH?

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:*Context.xml 
    </param-value>
</context-param>

This solution assumes that obviously all required modules are on the CLASSPATH but also that non-required modules aren't there.

Upvotes: 2

Related Questions