Spring Integration Hello Example


In this post we will create a simple example in Spring Integration with greeting message or hello message. Spring Integration aims to provide a clear line between code and configuration. The components provided by the framework, which often represent the enterprise integration patterns, are typically configured in a declarative way using either XML or Java annotations as metadata.

We will use here annotation or Java based configurations for our example on Spring Integration.

I would not focus on what is Spring Integration? or what are the advantages of Spring Integration? But you will find very good documentation on Spring Integration.


Eclipse 4.12, Java 12 (or 8), Spring Boot 2.1.8, Gradle 5.6

Creating Project

Create a gradle based project in Eclipse with the project’s name as spring-integration-hello.

Updating Build Script

The default generated build.gradle script has to be updated to include the required dependencies.

We have only one dependency with Spring Integration in the below build script.

buildscript {
	ext {
		springBootVersion = '2.1.8.RELEASE'
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

Creating Interface

We will create an interface that will provide a simple method sayHello() to say anyone a hello message.

To simplify the caller’s interaction we use gateway proxy using @MessagingGateway annotation that provides an Integration Messaging Gateway Proxy as an abstraction over the messaging API. The target application’s business logic may be completely unaware of the Spring Integration API, with the code interacting only via the interface.

The gateway element refers to a service interface. This is similar to the way the Spring Framework handles remoting.

The caller should only need to be aware of an interface, while the framework creates a proxy that implements that interface.

The proxy is responsible for handling the underlying concerns such as serialization and remote invocation. In this case, it does message construction and delivery.

package com.roytuts.spring.integration.hello.service;

import org.springframework.integration.annotation.MessagingGateway;

@MessagingGateway(name = "helloGateway", defaultRequestChannel = "channel")
public interface HelloService {

	String sayHello(String name);


Creating Implementation Class

The corresponding implementation class for the above interface is given below:

package com.roytuts.spring.integration.hello.service.impl;

import com.roytuts.spring.integration.hello.service.HelloService;

public class HelloServiceImpl implements HelloService {

	public String sayHello(String name) {
		return "Hello " + name;


Creating Spring Config Class

We will create Spring configuration class to provide annotation based metadata configurations.

We create MessagingChannel to connect application to messaging system. But here we are not going to use any JMS system.

A Service Activator is a component that invokes a service based on an incoming message and sends an outbound message based on the return value of this service invocation.

The service activator is connected to a Spring Integration MessageChannel within the ApplicationContext. Any component can send messages to this service activator’s input-channel. The service activator doesn’t require any awareness or make any assumption about that sending component.

package com.roytuts.spring.integration.hello.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

import com.roytuts.spring.integration.hello.service.HelloService;
import com.roytuts.spring.integration.hello.service.impl.HelloServiceImpl;

public class SpringConfig {

	public MessageChannel channel() {
		return new DirectChannel();

	public HelloService helloService() {
		return new HelloServiceImpl();

	@ServiceActivator(inputChannel = "channel")
	public MessageHandler serviceActivator() {
		// System.out.println("helloService: " + helloService());
		return new ServiceActivatingHandler(helloService(), "sayHello");

Creating Main Class

We know that main class is enough to run the Spring Boot application whether it is a standalone or web based application.

We use @Qualifier annotation to qualify the gateway proxy instead of service implementation.

We have implemented CommandLinerRunner to run a block of code only once in application’s lifetime after initialization of the application.

package com.roytuts.spring.integration.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.roytuts.spring.integration.hello.service.HelloService;

@SpringBootApplication(scanBasePackages = "com.roytuts.spring.integration.hello")
public class SpringIntegrationHelloApp implements CommandLineRunner {

	private HelloService helloGateway;

	public static void main(String[] args) {
		SpringApplication.run(SpringIntegrationHelloApp.class, args);

	public void run(String... args) throws Exception {
		// System.out.println("helloGateway: " + helloGateway);


Testing the Application

By executing the above main class you will see below output in the console:

Hello Soumitra

So we learned here how Spring Integration addresses a messaging within a single application.

Source Code

Download Source Code

Thanks for reading.

Leave a Reply

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