Results 1 to 4 of 4

Thread: multiple application context unit test.

  1. #1
    Join Date
    Feb 2007
    Posts
    291

    Default multiple application context unit test.

    I have a particular problem. I have multiple jar files containing applicationContext of the dao definition and services including properties file.

    I want to use spring, hibernate and unit test the multiple jar files.

    How should I do this.

    Example
    Jar 1 has applicationContextA.xml , hibernate.cfg.xml, applicationContextA.properties.

    Jar 2 has applicationContextB.xml , hibernate.cfg.xml, applicationContextB.properties

    I have a new project that is going to build a war file having both jar 1 and jar 2 and i want to have applicationContextC.xml include both hibernate.cfg.xml and both applicationContextA and applicationContextB.xml

  2. #2
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    87

    Default

    I'm only a newbie but i think that it's possible with some modifications.

    I think that we should rename one hibernate.cfg.xml.

    web.xml
    ....
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>

    Note:
    If you use tomcat as application server i don't think that would work if the database are different but i don't sure.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Your hibernate cfg is going to be problematic, however the rest shouldn't be a problem. If you use the ContextLoaderListener you can configure it to load all the xml files from that classpath. Especially if you use some naming convention that can be handy.

    Code:
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
    Your different properties should be normally loaded by their respective modules (they are the ones needing it not your web application).

    Hibernate can be problematic however it depends if ou need 1 or more hibernate session factories.
    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

  4. #4
    Join Date
    Mar 2008
    Posts
    7

    Default use explicit naming of the contexts and not wildcards

    While the wildcard solution is quicker and more flexible, I would suggest that you explicitly indentify the contexts that you require. First create a context specific to your web application.

    Code:
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext-[myapplication]*.xml</param-value>
    </context-param>
    This applicationContext-[myapplication]*.xml file would then import the specific context files that the application expects.

    Code:
    <beans>
    	<import resource="/applicationContextA.xml"/>
    	<import resource="/applicationContextB.xml"/>
    	<import resource="/applicationContextC.xml"/>
    </beans>
    In this example, i would expect jar 1 and jar 2 to have different internal package structures. In the case where the spring context files are packages along side the java source, this whould allow the same sping context file name to be used in two different jars, and still be uniquely identified.

    Code:
    <beans>
    	<import resource="/com/project/sub-project-1/applicationContextA.xml"/>
    	<import resource="/com/project/sub-project-2/applicationContextA.xml"/>
    </beans>
    Does anybody have a link to recommended or best practice guidelines for naming and packaging spring context files?

Posting Permissions

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