File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Application Frameworks and the fly likes Spring MVC read again applicationContext.xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring MVC read again applicationContext.xml?" Watch "Spring MVC read again applicationContext.xml?" New topic

Spring MVC read again applicationContext.xml?

Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Hi all,

I have a curious problem... When the Tomcat starts my application, the ContextLoader read my bean definitions in the applicationContext(we split in some files), and when the Spring DispatcherServlet start, read again!

So the problem is, after DispatcherServlet read, I take two hibernate Session Factorys! The session factory created by ContextLoader and the session factory created by DispatcherServlet. That cause a big problem. Somebody know why this happen? And how avoid the second read?

The log:

The Hibernate Configuration

The applicationContext.xml (that import some other files, that contain beans definitions)

In some application file that was imported, I have the bean definition that is extended by all the DAO classes:

[ September 28, 2007: Message edited by: Luciano A. Pozzo ]
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
I found the problem. So I am posting the solution bacause somebody could have the same situation:

My application context was divided in some files for make easy the maintenance. And these files had been grouped in a main file through the tag import of spring.

The problem is that the ContextLoaderListener read and load the bean definitions of this "main file" and after the DispatcherServlet of spring mvc do the same thing again. So what happen?! I have two differents sessionFactory objects because was load twice. Then the OSIV do not work, because always open and close one session. That it is not the one used by the Persistence Manager.

So, what I did to solve. I divided the main file in two. One with the spring mvc definitions (beans that spring mvc need). And other with my business and data access (includes the sessionFactory). So, the Spring DispacherServlet load the definitions that need and the ContextLoaderListener the other one, and everything is loaded once and works.

Pay attention that Spring DispactherServlet read the xml using the name of the servlet (that you defined) more -servlet.xml. Example:

(Your xml should be dispatcher-servlet.xml)

And in your contextConfigLocation, put the other xml. Like this:

Hope that it's clear.

I agree. Here's the link:
subject: Spring MVC read again applicationContext.xml?
jQuery in Action, 3rd edition