What's Next: View All Tutorial →

Spring Framework Hello World

A simple program to retrieve Spring bean and prints a “hello world” string. Technology used in the Article: 1. Spring 4 2. Maven 3. Netbean 8 IDE Note: The Maven plugin will come together with the Netbean IDE, There is no ...

Published Date: 09/12/2014  Last Modified Date: 09/12/2014    New Post

A simple program to retrieve Spring bean and prints a “hello world” string.

Technology used in the Article:

1. Spring 4
2. Maven
3. Netbean 8 IDE

Note: The Maven plugin will come together with the Netbean IDE, There is no additional configuration required for maven to setup in Netbeans IDE. Maven is used only for downloading the required spring jar to the project.

Netbean IDE is similar to eclipse IDE. To use the same short key as eclipse in Netbean IDE, Go to Tools -> option -> Keymap -> Profile - >Eclipse

1. Create a new Maven Java application in Netbeans IDE

Go to File -> New Project -> Maven -> Java Application
Enter the Project Name: SpringHelloWorld
GroupId: com.tutorial
Click "Finish" to create a project

2. Add Spring library to download required spring jar using maven

Open the pom.xml file, 

Add the spring-context dependency

<dependecy> should be added inside the <dependencies>

pom.xml(Full content)
<?xml version="1.0" encoding="UTF-8"?>
<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">


        <!-- Spring framework -->


3. Create the resources folder for Maven Projects

The resouces folder will not be created by default, To create the resource folder

Go to NetBeans Editor -> Windows -> Files

The folder view will be opened. Expand the folder src and main.

Create a new folder "resources" under the folder main.

After the resources folder created Go to the projects view, and you can see "Others sources (src/main/resources)" 

Alternatively, we can create the resources folder in path "src/main/resources" where the project is created.

4. Spring bean (Java class)

HelloWorld.java (in src/main/java/com/tutorial/springhelloworld)
package com.tutorial.springhelloworld;

public class HelloWorld {

    private String name;

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

    public void printHello() {
        System.out.println("Hello ! " + name);

5. Spring bean configuration file

Spring bean configruation file contains all the bean creation, instantiation releated information

Create a new file SpringBean.xml in the resources folder
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    <bean id="helloBean" class="com.tutorial.springhelloworld.HelloWorld">
        <property name="name" value="Catchmycity" />

6. Program Execution

Run Application.java, it will load the Spring bean configuration file (SpringBean.xml) and retrieve the Spring bean via getBean() method.

ApplicationContext is used to load the Spring bean into the memory
ClassPathXmlApplicationContext will point to the resources folder, the files inside the resources folder can be access using this

If the SpringBean.xml stored in any other place, we can use FileSystemXmlApplicationContext to get the path

File: Application.java  (in src/main/java/com/tutorial/springhelloworld)
package com.tutorial.springhelloworld;

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

public class Application {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBean.xml");

        HelloWorld obj = (HelloWorld) context.getBean("helloBean");

In this example, The HelloWorld object is initialized from the Spring bean, the one-time instantiated object is retrieved using the context.getBean() bean and we can invoke the printHello() method of the HelloWorld class

7. Output

Right click and Run the Application.java file
--- exec-maven-plugin:1.2.1:exec (default-cli) @ SpringHelloWorld ---
Dec 10, 2014 12:59:09 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@176fe71: startup date [Wed Dec 10 00:59:09 IST 2014]; root of context hierarchy
Dec 10, 2014 12:59:09 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [SpringBean.xml]
Hello ! Catchmycity

Awaiting for Administrator approval

Tags: [29]

← Back to list

Related Post