Hi all,

I have a general question about Spring when using it to develop a Java library (and not an application as such). I am sure it's pretty basic but I could not find relevant information easily on the web.

I am writing a library and write my code with Spring in mind, i.e., in the application where the library will be used, my classes can be used with Spring IoC, etc. For my unit tests, I have individual Spring application contexts that I run using @ContextConfiguration. That works nicely.

The question is this: Will the final application's applicationContext.xml instantiate all the beans, including the beans of my library, or do I provide a Spring context within the library that the application's applicationContext.xml will import?

I am not so happy with either of these approaches: The former forces me – the developer of the library – to expose some implementation details. For instance, there might be a particular bean that is currently used as a temporary solution and if this bean needs to be instantiated in the application's applicationContext.xml, that breaks modularity.

On the other hand, the latter approach –*importing the library's context from the application's applicationContext.xml –*may easily lead to clashes of bean names if I my library happens to use (or, in the future, introduces) a bean with a name that is also used in the application.

What is the Spring way to handle such dependencies?

Thanks a lot,
Kaspar