Hello, Spring! - Getting started with Spring framework


We will be using the following software/tools for this example:

  • jdk or jre 1.8.x
  • Eclipse IDE (As of this date, its neon.3)
  • Maven plugin, built into the eclipse

Step 1: Create a new Maven project in Eclipse

Create a new Maven project

Make sure to tick the checkmark "Create a simple project (skip archetype selection)"

Create a new Maven project

Group Id and Artifact Id should be given in package name convention:

Create a new Maven project

Add the following dependency to the pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>

Step 2: Create the contract interface and its implementation classes:

Spring encourages coding for interfaces concept, as it helps in loose coupling. Following is the HelloService interface we will be using:

package spring.demo.service;

public interface HelloService {
    public String greet(String user);
}

Let's create two implementation for the same:

package spring.demo.service;

public class EnglishHelloService implements HelloService {
    public String greet(String user) {
        return String.format("Hello, %s!", user);
    }
}

and

package spring.demo.service;

public class FrenchHelloService implements HelloService {
    public String greet(String user) {
        return String.format("Bonjour, %s!", user);
    }
}

Step 3: Bean factory configuration:

We can configure our Spring container to use these classes via an XML or an annotated Java class. First let's have a look at an minimalistic XML based configuration:

Filename: app.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="ehs" class="spring.demo.service.EnglishHelloService" />
    <bean id="fhs" class="spring.demo.service.FrenchHelloService" />
</beans>

Step 4: Create spring container and use the service beans:

Finally, we create a client application to create a spring container, get an instance for HelloService and use its service:

package spring.demo.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.demo.service.HelloService;

public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx;
        HelloService service;
        String message;


        ctx  = new ClassPathXmlApplicationContext("app.xml");
        service = ctx.getBean("ehs", HelloService.class);
        message = service.greet("Vinod");
        System.out.println("Message = " + message);

        service = ctx.getBean("fhs", HelloService.class);
        message = service.greet("Vinod");
        System.out.println("Message = " + message);

        ((AbstractApplicationContext) ctx).close();
    }

}

When you execute this program, here is what you get:

Jul 24, 2017 7:30:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Mon Jul 24 19:30:41 IST 2017]; root of context hierarchy
Jul 24, 2017 7:30:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [app.xml]
Message = Hello, Vinod!
Message = Bonjour, Vinod!
Jul 24, 2017 7:30:41 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Mon Jul 24 19:30:41 IST 2017]; root of context hierarchy

Instead of the old fashioned XML configuration, we may as well use an annotated Java configuration file as listed below:

package spring.demo.cfg;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import spring.demo.service.EnglishHelloService;
import spring.demo.service.FrenchHelloService;
import spring.demo.service.HelloService;

@Configuration
public class AppConfig {
    @Bean(name="ehs")
    public HelloService service1(){
        return new EnglishHelloService();
    }

    @Bean(name="fhs")
    public HelloService service2(){
        return new FrenchHelloService();
    }
}

The only line to change in the Main.java is the initialization of the variable ctx:

ctx  = new AnnotationConfigApplicationContext(AppConfig.class);

Just rerun the code, and get the same output.

That's all folks!

Vinod.


Courses

Blog posts