Spring Hello World

We will create a simple HelloWorld application using spring –

Tools used to create below project – 

  1. Spring 5.0.7.RELEASE
  2. Maven 3.3
  3. Java 8
  4. Eclipse Neon.2

Github Link :   Download

Step 1: Go to File -> New -> Project -> Maven Project . Select maven-archetype-quickstart and click Next.


 

 

 

 

 

 

 

 

Then you need to configure project name, group, artifact and package as shown below –


 

 

 

 

 

 

 

 

 

Step 2: Configure maven dependencies

Below pom.xml file contains the dependencies required for spring project –

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

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

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.7.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- Spring 5 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>

Below project structure will be created for you by Eclipse .


 

 

 

 

 

 

 

Step 3:  Create HelloWorldBean.java under com.myjavablog.beans package –

package com.myjavablog.beans;

/**
* Hello world!
*
*/
public class HelloWorldBean {
String name;

public void printHello() {
System.out.println("Hello World from " + name);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

This is the bean file where we have getters and setters defined.

Step 4: Create resources folder /src/main directory –

HelloWorld.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/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="HelloWorldBean" class="com.myjavablog.beans.HelloWorldBean">
<property name="name" value="Anup" />
</bean>

</beans>

This contains XML configuration for all the beans which we need to inject into our application.

Bean tag has attributes like –

id = Bean ID for unique identification

Class = Associate the bean class

property = To initialize various attributes of the bean

Step 5: Create a HelloWorldMain.java under com.myjavablog package –

package com.myjavablog;

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

import com.myjavablog.beans.HelloWorldBean;

/**
* @author anupb
*
*/
public class HelloWorldMain {

/**
* @param args
*/
public static void main(String[] args) {

ApplicationContext beanFactory = new ClassPathXmlApplicationContext("HelloWorld.xml");

HelloWorldBean beanInstance = (HelloWorldBean) beanFactory.getBean("HelloWorldBean");
beanInstance.printHello();
}

}

We have create ApplicationContext which actually loads XML bean definitions from class path resource i.e. HelloWorld.xml

Step 6: Clean and install project

Now run the maven clean install phase as below –

Command:  mvn clean install

OR

Right click on project -> Debug As -> Maven build . Below popup will come up –


 

 

 

 

 

 

 

Step 7: Run the spring application

 

 

 

 

 

 

 

 

Output:

Hello World from Anup

Leave a Comment

Bitnami