Announcement Announcement Module
No announcement yet.
move hibernate and datasource config to separate file? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • move hibernate and datasource config to separate file?

    i dont want to clutter the myapp-servlet.xml file with my hibernate/datasource configuration, what is the preferred way to move it into a separate file? thanks

  • #2
    A common practice is that the *-servlet.xml only contains that stuff that is related to the web tier. So your Controllers, Views etc.

    The file, typically, called applicationContext.xml contains everything which is needed to execute your business logic your services, daos and infrastructure code (datasource etc.).

    The applicationContext.xml file is loaded by a class called the ContextLoaderListener, the other file is loaded by your DispatcherServlet.


    • #3
      To elaborate on Marten's response, create a config file called something like spring-persistence.xml with the hibernate and db settings. Include this in your applicationContext.xml file like this:

      <import resource="classpath*:spring-persistence.xml"/>

      Spring is even able to find the spring-persistence.xml file inside an an inculded jar!

      This way you have a service layer (defined in the applicationContext.xml) in between the web and the persistence tier which could be used to coordinate operations that span multiple DAOs. It could also encapsulate common business logic used by multiple web controllers.

      With that architecture, applicationContext.xml would include spring-persistence.xml. Each layer would be unit testable (mock the DAOs when testing the service layer using easymock). Use AbstractTransactionalDataSourceSpringContextTests when testing the persistence layer to ensure idempotentcy of tests.
      Last edited by sambrodkin; Apr 9th, 2008, 02:53 AM.


      • #4
        if i have my applicationContext.xml file contain my data source/ dao/ services, and then i load that via contextConfigLocation, and my applicationContext.xml is in myapp/war/WEB-INF/applicationContext.xml (next to my web.xml, then how do i reference that directory? do i need to even specify a directory or can i just do <param-value>applicationContext.xml</param-value>


        • #5
          Maybe this thread is of any help:


          • #6
            in that thread he uses the path /WEB-INF/application-context.xml, but in my app its located in /war/WEB-INF/, so is that what i should use?