Hello World Using Spring Integration

Hello World Using Spring Integration


Spring Integration is a very powerful module in the Spring Framework. It was originally inspired by pivotal book in computer science called Enterprise Integration Patterns written by Gregor Hohpe and Bobby Woolf. While the Gang of Four focused their work on software design patterns, Enterprise Integration Patterns is focused on how disparate systems communicate with each other. As the industry has moved to highly scalable systems adopting cloud computing paradigms, the principles set forth in Enterprise Integration Patterns are highly relevant to Java programming.

Similar to the GoF focus, Spring Core largely focused on development within a single application or JVM environment.  Spring Integration builds upon the core principles of Spring Core to extend the Spring programming model to a message driven programming model. Abstracted from the developer when making a method call is the implementation details. Does it really matter if the method you are calling is a simple method call, a web service, a JMS Message, or making a request to the database? No, it should not. And more importantly, the implementation details should not leak back into your code.

In this example, I’ll step through the use of a Spring Integration Message Gateway for a classic “Hello World” programming example. I’ll show you how to use a Spring Integration Messaging Gateway for a simple send message, a request / response example, and an async request response example.

Spring Integration Hello World Code Example

Hello World Service Bean

For our example, we’re going to use a very simple service bean to either say hello or get a hello message. In a real world example, this code could be a direct method call, behind a web service, or perhaps its a message driven bean off a AQMP queue.

Spring Integration Gateway

The next component we need to setup is an interface class, which we will use for the Spring Integration Gateways. When coding to use Spring Integration, messaging gateways are a very convenient programming option to use. They have a very minimal impact on your application code. As the developer, you only need to create the interface specification. Spring Integration will provide the actual implementation and inject it into your class at run time. There is one small bit of leakage into your application code with the @Gateway  annotation. I could have coded this example without the annotations, but I find them very convenient to use. This annotation directs Spring Integration which messaging channel to use for the method call of the interface.

Spring Integration Gateway Configuration

The messaging gateway needs to be defined in your Spring Integration configuration. Spring Integration will not create the gateway bean without this configuration line.

Spring Integration Channel

Spring Integration Channels are a core concept to Spring Integration. They are used to decouple message producers (our gateway) from message consumers (our hello world bean). This abstraction is important because the channel could be a direct method call, or a JMS queue. The important take away is the decoupling. Neither the message producer nor the message consumer is aware of details about the implementation of the messaging channel.

In XML configuration snippet below, I’ve defined two messaging channels. Spring Integration has a number of options for defining messaging channels. The default channel is a direct channel, which is perfect for our hello world example.

Spring Integration Service Activator

Spring Integration has a concept of Service Activators. This is basically a way to configure a message consumer on a channel. This is completely decoupled from the gateway. Its important to remember the service activator is associated with a channel, and not a gateway or a gateway method. When you’re new to Spring Integration, it can be easy to lose site of this important distinction.

In this XML configuration snippet, I’ve defined two service activators.

Spring Integration XML Configuration

The complete XML configuration used in our hello world example follows below. You’ll see that I’m using a component scan to create the Hello World Service bean in the Spring context.

Running the Spring Integration Hello World Example

I’ve setup two examples to run our Spring Integration Hello World example. One using Spring Boot, and the second using JUnit. 

Spring Boot Application

In this example, I’m using Spring Boot to bring up the Spring Context. Since I placed the Spring Integration configuration in a XML configuration file, I need to use the @ImportResource  annotation to tell Spring Boot to import the XML configuration file.  In the code, I ask the Spring Context for an instance of the gateway bean. Then I call our three methods. The first is a simple send message with no return value. Our messaging service bean will write the hello message to the console. In the second example, I’m getting a message back from the Hello World Service bean.  The final example is a little more interesting. In this case, I’ve setup an asynchronous call. By specifying a Future in the return type, Spring Integration will automatically make a asynchronous call. This is an ideal technique to use when you have a longer running call (such as a remote web service), and you can do other work while the call is being made.

When you execute the above code, you will receive the following output in the console.

JUnit Example

We can run the same code as a JUnit test as well. In this case, I’m allowing Spring to autowire the Spring Integration gateway into my test. Then I have three separate tests to execute our three different hello world examples.

This is more typical of how you would use a Spring Integration gateway in your application code. You can utilize dependency injection in Spring to inject an instance of the Spring Integration gateway instance into your class at run time.

When you run the JUnit tests in this class, you will get the following output in the console.


This has been a very simple Hello World example using Spring Integration. While this post only scratches the surface of the abilities of Spring Integration, I hope you can see how easy it is to utilize Spring Integration in your code. As you learn to develop large scale enterprise applications, you will find that Spring Integration is an important module in the Spring Framework.

Get The Code

I’ve committed the source code for this post to github. It is a Maven project which you can download and build. If you wish to learn more about the Spring Framework, I am a free introduction to Spring tutorial. You can sign up for this tutorial in the section below.

Source Code

The source code for this post is available on github. You can download it here.


About jt

    You May Also Like

    Leave a Reply