Results 1 to 6 of 6

Thread: move hibernate and datasource config to separate file?

  1. #1
    Join Date
    Nov 2007
    Location
    Austin, TX USA
    Posts
    154

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2006
    Location
    Rotterdam
    Posts
    58

    Default

    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 at 02:53 AM.

  4. #4
    Join Date
    Nov 2007
    Location
    Austin, TX USA
    Posts
    154

    Default

    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. #5
    Join Date
    Apr 2008
    Posts
    6

  6. #6
    Join Date
    Nov 2007
    Location
    Austin, TX USA
    Posts
    154

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •