First Spring application example

15 December 2012
By Gonçalo Marques
This tutorial will demonstrate how to create a hello world Spring application.

Used software and configuration

This tutorial considers the following software and environment:

  1. Ubuntu 12.04
  2. Maven 3.0.4
  3. JDK 1.7.0.09
  4. Spring 3.2.0

Configure Maven to get the required Spring dependencies:

Maven pom.xml file referencing Spring dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.byteslounge.spring</groupId>
  <artifactId>com-byteslounge-spring</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>com-byteslounge-spring</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- Define Spring version as a constant -->
    <spring.version>3.2.0.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
  </dependencies>
</project>

Now place yourself in the project directory and issue the following command to prepare your project for Eclipse:

mvn eclipse:eclipse

After conclusion you can import the project into Eclipse.

Classes definition

We need to define our classes. Let's create a simple spring bean:

ExampleBean.java
package com.byteslounge.spring;

import org.springframework.stereotype.Service;

@Service
public class ExampleBean {

  public String sayHello() {
    return "Hello!";
  }

}

Please note the @Service annotation. This annotation is used to define this class as a bean managed by Spring. We will come back to this later in the tutorial.

Now we define our main class:

Main.java
package com.byteslounge.spring;

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

public class Main 
{
  public static void main( String[] args )
  {
    ApplicationContext ctx = 
                    new ClassPathXmlApplicationContext("spring.xml");
    ExampleBean exampleBean = (ExampleBean) ctx.getBean("exampleBean");
    System.out.println(exampleBean.sayHello());
  }
}

The main class is basically initializing an application context based on a configuration file: spring.xml. We will see this file in the next section. After the Spring context is initialized we use it to fetch our Example bean from the Spring application context. Note that we used exampleBean as the bean name to fetch it from the context. This is how Spring defines its managed beans names if we don't specifically define a name. It converts the bean class name into a camel case format. In our case ExampleBean was named exampleBean. We used ClassPathXmlApplicationContext to lookup our Spring configuration file. This specific application context loading implementation looks for the configuration file in the application classpath, as the name suggests. We will define this configuration file in the next section.

Spring configuration

We already know by the previous sections that we need a Spring configuration file named spring.xml in our application classpath. We are using Maven so we need to create it in /src/main/resources/ because this directory is used by Maven to package application resource files. Our spring.xmlconfiguration file looks like the following:

spring.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="com.byteslounge.spring" />
    
</beans>

Here we are instructing the Spring container to scan com.byteslounge.spring package for beans. Since our ExampleBean class is inside this package and is annotated with @Service annotation, Spring will know that it is a bean and will instantiate it and make it available to the application. The bean is then fetched in the Main class as we have seen in the previous tutorial section.

Our directory structure should look like the following by now:

Directory structure
Project folder structure

Running the application

Now when we run our Main class the following output is generated:

Hello!

We have successfully run our first Spring application! The example source code is available for download at the end of this page.

We used annotations for configuring our Spring bean. In the next section we will see how the configuration could have been done without annotations and using the Spring XML configuration file instead.

Alternate bean configuration (XML)

We could have configured our Spring bean using XML instead of annotations. To do this we remove the @Service annotation from our bean class:

ExampleBean.java without @Service annotation
package com.byteslounge.spring;

public class ExampleBean {

  public String sayHello() {
    return "Hello!";
  }

}

And we configure our bean in Spring XML configuration file. Note that we included the bean and removed the package scanning:

spring.xml including bean configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <bean id="exampleBean" class="com.byteslounge.spring.ExampleBean" />
    
</beans>

Download source code from this article

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: http://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: