Spring Tutorial for Beginners

1- Introduction

This document is written based on:
  • Spring Framework 4.x

  • Eclipse 4.6 NEON (ok for Eclipse 4.5 MARS)

Document History:
  • 15-05-2014: Based on Eclipse 4.4 LUNA + Spring 4.0.4 + Download Spring Library
  • 15-07-2015: Based on Eclipse 4.5 MARS + Spring 4.0.4 + Using Maven.
  • 17-09-2016: Based on Eclipse 4.6 NEON + Spring 4.0.4 + Using Maven + Annotation Config.
In this document, I use Maven to declare the Spring library will use, instead of downloading Spring library and declaring in the normal way.

Maven is a tool that help you manage your library automatically and efficiently, and it has become customary which any Java programmers must know. If you do not know about Maven you can spend 10 minutes to learn how to use it here:
In case you want to download Spring and declare the library in the traditional way you can see the appendix at the end of the document.

2- Spring Framework

The illustration below shows the structure of the Spring Framework.
  1. IoC Container: This is the most important and also the basis, the foundation of Spring. It is the role of configuration and lifecycle management of Java objects. Posts Today we will learn about this part.
  2. DAO, ORM, AOP, WEB: The module is available tool or framework is integrated into the Spring.

3- Create Maven Project

  • File/New/Other...
  • Group Id: org.o7planning
  • Artifact Id: HelloSpringAnnotation
  • package: org.o7planning.spring
Your project has been created:
Ensure that your Project is built on Java 7 or newer. Right-click the project and select Properties.

4- Declaring basic library of Spring

This is HelloWorld Spring example, so we just use the basic Spring library (Core). Open the pom.xml file to declare the library will use:
<project xmlns="http://maven.apache.org/POM/4.0.0"






        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->

        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->


5- Code Project

Below is an illustration of the project structure:
package org.o7planning.spring.lang;

// A Language
public interface Language {

    // Get a greeting
    public String getGreeting();

    // Get a bye
    public String getBye();

package org.o7planning.spring.lang.impl;

import org.o7planning.spring.lang.Language;

// English
public class English  implements Language {

 public String getGreeting() {
     return "Hello";

 public String getBye() {
     return "Bye bye";

package org.o7planning.spring.lang.impl;

import org.o7planning.spring.lang.Language;

// Vietnamese
public class Vietnamese implements Language {

   public String getGreeting() {
       return "Xin Chao";

   public String getBye() {
       return "Tam Biet";

@Service is an annotation which is used to annotate on a class to inform Spring that the class is a Spring BEAN.

@Autowired is annotated on a field to inform Spring that let's inject value into the field. Note: The meaning of term "inject" is similar to assigning a value to that field.
package org.o7planning.spring.bean;

import org.o7planning.spring.lang.Language;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class GreetingService {

   private Language language;

   public GreetingService() {


   public void sayGreeting() {
       String greeting = language.getGreeting();

       System.out.println("Greeting: " + greeting);

@Repository is an annotation which is used to annotate on a class to inform Spring that the class is a Spring BEAN.
package org.o7planning.spring.bean;

import java.util.Date;

import org.springframework.stereotype.Repository;

public class MyRepository {

    public String getAppName() {
        return "Hello Spring App";

    public Date getSystemDateTime() {
        return new Date();
@Component is an annotation which is used to annotate on a class to inform Spring that the class is a Spring BEAN.

@Autowired is annotated on a field to inform Spring that let's inject value into the field. Note: The meaning of term "inject" is similar to assigning a value to that field.
package org.o7planning.spring.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class MyComponent {
    private MyRepository repository;

    public void showAppInfo() {
        System.out.println("Now is: "+ repository.getSystemDateTime());
        System.out.println("App Name: "+ repository.getAppName());

There is no difference between the usage of @Service, @Component and @Repository, you can use them to annotate on your class that is suitable for the meaning and context of the application.

6- Spring @Configuration & IoC

@Configuration is an annotation which is annotated in a class defining Spring BEANs.

@ComponentScan - Deliver report to Spring about packages to search for other Spring BEANs, Spring will scan the packages to search for.
package org.o7planning.spring.config;

import org.o7planning.spring.lang.Language;
import org.o7planning.spring.lang.impl.Vietnamese;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class AppConfiguration {

    @Bean(name ="language")
    public Language getLanguage() {

        return new Vietnamese();
Spring BEANs that are created will be managed in Spring IoC Container.

7- Spring ApplicationContext

package org.o7planning.spring;

import org.o7planning.spring.bean.GreetingService;
import org.o7planning.spring.bean.MyComponent;
import org.o7planning.spring.config.AppConfiguration;
import org.o7planning.spring.lang.Language;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainProgram {

   public static void main(String[] args) {

       // Creating a Context Application object by reading
       // the configuration of the 'AppConfiguration' class.
       ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);

       Language language = (Language) context.getBean("language");
       System.out.println("Bean Language: "+ language);
       System.out.println("Call language.sayBye(): "+ language.getBye());
       GreetingService service = (GreetingService) context.getBean("greetingService");

       MyComponent myComponent = (MyComponent) context.getBean("myComponent");
Run MainProgram class

8- Working Principle of Spring

You create an ApplicationContext object by reading configures in class AppConfiguration. For example, see the code below.
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);
Spring will create Spring BEANs according to the definitions in class AppConfiguration (Note: AppConfiguration class must be annotated by @Configuration).
Next, Spring will search in package "org.o7planning.spring.bean" in order to create other Spring BEANs (Create objects from classes annotated by @Service, @Component or @Repository).
Spring BEAN is now created and is contained in Spring IoC. The fields of Spring BEANs are annotated by @Autowired that will have values injected. Let's see illustration below:
So, what is IoC?
In case that an object is created from a class traditionally, its fields have value assigned inside the class. Reversely, for Spring, its objects and fields have value injected from the outside by an object called as IoC.

IoC stands for "Inversion of Control" 

IoC Container is a container containing all Spring BEANs used in the application.

9- Programming Web Application using Spring MVC

Next you can learn programming web applications with Spring MVC:

10- Appendix: Download Spring Library

You can download Spring at:
Unzip the downloaded zip file to a hard drive folder: