Version 1

    Why need Single Spring Application Context

    Here I would like to describe how I solved one of the issues through JBoss Modules.

    Often I thought how it is better to use Spring framework under JBoss/Wildfly. I believe that you also faced with situation when you need to use a few Spring Contexts in the different web applications, however you need to have single Spring Context that could contain common beans for others contexts. It would be great to load this guy only once, because I don't want to have any duplicates of beans.

    Spring Framework via JBoss Modules

    The best practice is using JBoss Modules instead of jars under WEB-INF/libs. I didn't find any plugins for gradle to be able to create a module.xml with all dependencies and jar files which are needed for this module.

    I have made https://github.com/zhurlik/gradle-jboss-modules-plugin that allows you to use something like this to define a JBoss Module for Spring Core:

    springCore {

      moduleName = 'org.springframework.core'

      resources = ["spring-core-${springVersion}.jar"]

      dependencies = ['javax.api',

      'org.jboss.vfs',

      'org.apache.commons.logging'

      ]

      }

    You can look at another link https://github.com/zhurlik/jboss-wildfly-modules where collected modules to extract Spring and Apache Camel frameworks from web apps.

     

    Global Spring Context

    There is one more project https://github.com/zhurlik/spring-global-context that contains the JBoss Module named 'com.zhurlik' and a simple web application to show how it can be used.

    The main idea is based on https://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/

    It means that in your web applications you just need to do the following:

    • jboss-deployment-structure.xml
      <?xml version="1.0" encoding="UTF-8"?>

    <jboss-deployment-structure>

        <deployment>

            <dependencies>

                <module name="com.zhurlik" />

                <module name="org.springframework.core" />

                <module name="org.springframework.web" />

                <module name="org.springframework.context" />

                <module name="org.springframework.context-support" />

            </dependencies>

        </deployment>

    </jboss-deployment-structure>

    • web.xml

        <context-param>

            <param-name>parentContextKey</param-name>

            <param-value>rootContext</param-value>

        </context-param>

        <context-param>

            <param-name>locatorFactorySelector</param-name>

            <param-value>classpath*:global.xml</param-value>

        </context-param>

       

        <listener>

            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

        </listener>

    After this when 'com.zhurlik' will be added as a module to you JBoss/WildFly server you will have only one instance of Spring Context that was loaded in the module.

    That's not a big deal to register its as JNDI resource as well.

     

    I tested it using two web applications and saw single context that was loaded once.

    I hope that should be interesting, please let me know if you have any questions.