Thursday, 22 September 2016

Spring Basic Hello World Program

0 comments
Let's start learning Spring Framework with actual programing instead lengthy theory.

Program Definition

Write a simple Spring Application which will print "Hello World!" or any other message based on the configuration done in Spring Beans Configuration file.

Step-1 Create Maven Project in Eclipse IDE

Create Maven project in Eclipse IDE. 
File > New > Maven Project > Create Simple Project

Provide following details:
Group Id: com.iamlearninghere
Artifact Id: helloworld
Project Name: helloWorld

Click Finish. Now project setup is done

Step-2 Add Spring Library in pom.xml

pom.xml

<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.iamlearninghere</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework-version>4.1.4.RELEASE</org.springframework-version>
  </properties>

  <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
      
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

  
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

This will automatically download all required jar from Maven Repository

Step-3 Do Actual Code For Program

Create HelloMessage.java and modify App.java as below:

HelloMessage.java

package com.iamlearninghere.helloworld;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class HelloMessage  {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

App.java
package com.iamlearninghere.helloworld;

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

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
      
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        HelloMessage helloMessage = (HelloMessage)applicationContext.getBean("helloMessage");
      
        System.out.println("This is message: " + helloMessage.getMessage());
    }
}

  • Create Application Context using ClassPathXmlApplicationContaxt, This API load beans.xml configuration file from classpath. It will take care of creating, initializing and destroying beans.

Step-4 Create Bean Configuration File

Create beans.xml file under src folder to define beans(POJO).
beans.xml

<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-3.0.xsd">

   <bean id="helloMessage" class="com.iamlearninghere.helloworld.HelloMessage">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

Step-5 Running Program

Run App.java using Ctrl_F11 and if everything is write, it will print following output

Output:

This is message: Hello World!

Congratulations, you have created your first Spring Application successfully. Hope this helped to start with Spring Framework.
 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT