Bootstrapping a Spring Application Context in a JEE RAR/EAR

The way of loading a Spring Application Context in Java Web Applications and JSE applications is commonly known. For web applications packaged in WAR files, in a web.xml file need to be declared a ContextLoaderListener and a contextConfigLocation context parameter which refers to XML Spring Application Context configurations. On the other hand, for JSE applications a Spring Application Context is loaded manually in code.
This article tries to present how to design a Java EE application that gives a possibility to load a Spring Application Context without using a WebApplicationContext. It is not always required to have a web nature of a Java enterprise application.


It is feasible to load a Spring Application Context through a RAR. According to the Java EE specification the Resource Adapter Archive (RAR) is an archive that contains certain resources for Java EE Connector Architecture (JCA) applications. The exact explanation is available on [1]. The structure and packaging is presented on [2]. In a META-INF/ra.xml file inside a RAR, there is declared SpringContextResourceAdapter and specified a ContextConfigLocation property with value addressed to a Spring Application Context XML configuration. The generic idea and configuration is given on [3].

<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="" xmlns:xsi=""
 <vendor-name>Spring Framework</vendor-name>
 <eis-type>Spring Connector</eis-type>

Figure 1: RAR/META-INF/ra.xml example

In this example context configuration is placed somewhere under META-INF in a classpath.

1. Preparing a RAR

In order to prepare a RAR with a loadable Spring Application Context:

  1. Prepare a spring artifact with a Spring Application Context.
  2. Prepare a RAR resources following official guideline [2].
  3. In a RAR/META-INF/ra.xml set appropriate ContextConfigLocation to a Spring Application Context configuration location. The Spring Application Context can be included inside a RAR itself or can be placed in an outside resource when a RAR is deployed in an EAR.
  4. Assemble a RAR as a standalone archive or inside an EAR.

2. Demo system

A pretty simple system was designed to illustrate how a loadable Spring Application Context inside an EAR works in action.

There are three nodes:

JEE Application Server to run the,

JSE Application Client to run,

ActiveMQ Server to transfer messages between and
where plays role of server and plays role of client. The purpose of this system is to allow transfer money on server side on requests received from clients.

Figure 2: Deployment diagram of the demo system

System requirements:

  1. JEE 6+ JDK
  2. JEE 5+ compliant Application Server
  3. Apache ActiveMQ TM Server 5.8.0
  4. Apache Maven 3

It was developed and ran using followings:

  1. Oracle JDK javac 1.7.0_01
  2. Oracle WebLogic Server Version:
  3. Apache ActiveMQ TM Server 5.8.0
  4. Apache Maven 3.0.3

Server []

server source code [1]

There is presented a simple which uses a RAR to load a Spring Application Context. This is an EAR application ( which contains two spring artifacts (, and a RAR ( which refers directly to the main artifact ( Application receives requests asynchronously through a MessageListener and then responses sending a JMS message to a destination specified by a requester. Project has a Maven structure and is presented in Figure 3.

Figure 3: Component diagram of the

List of Maven artifacts:

  • – business logic to make a money transfer
  • – listener which receives requests and delegates them to the module and finally sends JMS responses; here is exposed the main Spring Application Context
  • – RAR module which declares SpringContextResourceAdapter and refers to the module; assembles a RAR
  • – EAR module; assembles the whole application into an EAR
  • – auxiliary artifact to build this project

In order to build this application:

  1. Go to artifact location on a command line and execute: mvn clean install
  2. Under location of in the target folder a will be created

Client []

client source code [2]

This is a simple Java client which sends request to make a money transfer. App has Maven artifact structure.

List of Maven artifacts:

  • – this is the only artifact of the client side

In order to build this application:

  1. Go to artifact location on a command line and execute: mvn clean install
  2. Under location of in the target folder a will be created

ActiveMQ Server

Apache ActiveMQ TM is the open source messaging and Integration Patterns server. It is used here to transfer messages between Money Transfer App and a JSE Client.

In order to run the messaging server:

  1. Prepare Apache ActiveMQ Server. It is possible to download it from [4]. We assume default settings, particularly broker URL – tcp://localhost:61616
  2. Start up the ActiveMQ Server by executing ACTIVE_MQ_SERVER_HOME_DIR/bin/ activemq.bat (


Now both the server and the client side are compiled and the messaging server is running.

The next steps:

  1. Deploy on a JEE Application Server
  2. Run JSE Client, go to the target folder of the artifact on a command line and execute: java -jar

The output should be similar to this:
code [-1], msg [null]
code [0], msg [OK]

3. Summary

This post could give an approach to prepare a Java EE application with a loadable Spring Application Context. The pivotal part is a configuration inside a RAR/META-INF/ra.xml file. Simple demo is a complete solution which uses a RAR assembled within an EAR. If you are interested in the simple demo system you can go for source code in the Resources section.


[1] What Is a Resource Adapter? –

[2] Packaging Resource Adapter Archives –

[3] Class Spring Application ContextResourceAdapter – Application ContextResourceAdapter.html

[4] ActiveMQ Server –


[1] Server [] | GitHub [softexploration/lab]

[2] Client [] | GitHub [softexploration/lab]

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>