Announcement Announcement Module
No announcement yet.
Unable to have separate config file for Hibernate/DAO beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to have separate config file for Hibernate/DAO beans

    I am developing an example Spring MVC application which is a variation of what is described in the Spring MVC step-by-step tutorial. I have a Spring/Hibernate DAO class which is working well and I want to use this as a property of my Controller class for use within its handleRequest() method. In this Controller class I have a DAO private member, and a setter method for Spring to be able to set this property. I have the Spring wiring configuration split into two files, myapp-data.xml and myapp-servlet.xml.

    The trouble is that when I run the application Spring croaks upon initializing the Controller bean because it can't find the definition for the DAO bean. It somehow knows how to find myapp-servlet.xml for the configuration of the Controller and ViewResolver beans, but it doesn't know to look for myapp-data.xml for the configuration of the DAO and HibernateTemplate beans.

    Am I correct in assuming that I should have all Spring configuration files (in this case myapp-data.xml and myapp-servlet.xml) in the WEB-INF directory? If not then what is the most common or best place for these Spring config files? Perhaps the non-MVC config files should go elsewhere?

    How does Spring magically know to look for myapp-servlet.xml for the configuration of the MVC components? I never specify this anywhere, so is this is a default convention?

    For now I can get around the trouble by putting all of the Spring bean configuration into the myapp-servlet.xml, but I'd like to be able to split these apart as recommended in "Spring in Action". Can anyone sugget how I should go about doing this?

    Thanks in advance for any suggestions.


  • #2

    You don't need to put all your configuration files in /WEB-INF/ but that is often what people will do. Check out the Petclinic sample application to see how multiple configuration files can be loaded - specifically check out the configuration for web.xml.



    • #3
      Thanks Rob.

      I also found a good explanation of this approach on pages 273-275 of "Spring in Action".