views:

496

answers:

2

I was just thinking that when should I actually consider loading more than one application contexts in Spring? All so far I have been merging the context files with <include> such that only one application context loads up.

Do you have an idea about when to go for more than one application contexts in the same JVM?

+4  A: 

When you need to use hierarchical contexts, for example - like Spring MVC does. Your "web" context is loaded separately from your "main" context, so stuff defined in "main" context (services / DAOs / etc) is available to "web" but not the other way around.

ChssPly76
I get your point. But do you have any idea that whether this results in some performance improvements etc.? Whats so bad even if the service context is loaded in the same context as web context? Just want to make it very clear to myself..
peakit
I haven't actually measured the performance but the difference should be insignificant. What is significant, however, is separation of concerns - your service should not know or care about your UI controller; and having hierarchical contexts prevents you from intentionally or accidentally injecting latter into former.
ChssPly76
Thanks for enlightening me about 'hierarchical contexts' !! I will do some research on it.
peakit
A: 

I almost always create many Spring configuration files to separate out the different layers of my application, but I always load them all via a single application context. The way I see it, I am using Spring for "application assembly", wiring together the various components of my application. Loading two different contexts, in my way of thinking, does not make sense. There is only one app, and thus only one context needed. Perhaps my view is simplistic.

SingleShot
Extending that line of thought a bit, "there is only one app, and thus only one class is needed. Which should really consist of only main() function" :-) Hierarchical contexts help maintain layer separation.
ChssPly76
Perhaps I am misunderstanding what you are saying because I certainly don't agree with your tongue-in-cheek example above. In a straight, non-web, Java app, I would only call "new xxxApplicationContext(springFiles)" once, but pass in many spring files. Are you saying there is a way to load multiple contexts and have relationships between them, or are you strictly speaking about the web MVC context being a special case (which I was not aware of)?
SingleShot
OK, I used "the googles" and see it is web-specific. Thanks for describing this - I've never encountered it. +1
SingleShot
You **can** load multiple separate contexts (they'll have to be hierarchical in order to have relationships and said relationships would only go one way - from child to parent); this does not depend on (although is normally used by) Spring MVC. Whether you would **want** to do it is a different question and the answer for "straight non-web java app" is probably "no". Thus my example, though obviously overly exaggerated, is appropriate - it all depends on complexity of the app in question.
ChssPly76
"the googles" lied to you :-) You can create hierarchical FileSystemXmlApplicationContext, for example: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/support/FileSystemXmlApplicationContext.html#FileSystemXmlApplicationContext(java.lang.String[], org.springframework.context.ApplicationContext)
ChssPly76
Well cool. I've used Spring for 4-5 years and never encountered this... Crazy. Thanks!
SingleShot