Announcement Announcement Module
Collapse
No announcement yet.
multiple application context unit test. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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?

        Comment

        Working...
        X