4.2. Spring as Shared Libraries

One method of using Spring inside Red Hat JBoss Enterprise Application Platform is by installing it as a shared library. This strategy is preferred when multiple applications using Spring are required to run on the same application server, as this strategy helps you to avoid including JARs in each deployed application. To install Spring as a shared library, create a Spring module in the EAP_HOME/modules/system/add-ons directory of the application server. A JBoss Enterprise Application Platform module is identified or referenced by its identifier and slot. This section, uses a module with the org.springframework.spring identifier, and the main slot. This is the default slot: if a slot name is unspecified, JBoss Enterprise Application Platform assumes a slot name of 'main' for identifying the module.

Procedure 4.1. Installing Spring as a Shared Library

  1. Create EAP_HOME/modules/system/add-ons/org/springframework/spring/main directory.
    This path consists of a main directory (org/springframework/spring) which reflects the module identifier and a subdirectory (main) which reflects the slot name.
  2. Copy the Spring JARs, to be shared, in the EAP_HOME/modules/system/add-ons/org/springframework/spring/main directory.
  3. Create a module.xml descriptor, which includes references to the Spring JARs and application server dependencies to the module. Each JAR copied to the module folder must have a distinct <resource-root> entry in the module descriptor.

    Example 4.1. Example module.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="org.springframework.spring" slot="main">
        <resources>
            <resource-root path="aopalliance.jar"/>
            <resource-root path="aspectjrt.jar"/>
            <resource-root path="aspectjweaver.jar"/>
            <resource-root path="spring-aop.jar"/>
            <resource-root path="spring-beans.jar"/>
            <resource-root path="spring-context.jar"/>
            <resource-root path="spring-context-support.jar"/>
            <resource-root path="spring-core.jar"/>
            <resource-root path="spring-expression.jar"/>
            <resource-root path="spring-web.jar"/>
        </resources>
      <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
        ...
      </dependencies>
    </module>
    
    
    The module name and slot must match the directory structure of the module.
JBoss Enterprise Application Platform allows you to create multiple Spring modules, with different identifier or slot name combinations. Thus, your applications can share multiple Spring versions at the same time, and the Spring versions do not interfere with each other. Also, you applications can point to any of the Spring modules.
Determining the JARs That Need to Be Copied
The set of JARs that you must install as shared libraries are the Spring JARs on which your applications are dependent. Also include AOP Alliance APIs and AspectJ libraries, if necessary.
Implications of Packaging the Application
If you install Spring libraries as shared libraries inside the application server, do not included the libraries in the deployed applications as that may lead to classloading errors. For Ant builds, do not include the Spring libraries in the <lib/> element of the WAR or EAR tasks. In Maven builds, set the scope to provided.