Spring 5 Hello World Example + XML Configuration

How to create a Spring project using Maven are demonstrated in this guide. In this Spring Hello World Example – XML Configuration are used.

Technologies and Tools Used for Spring Hello World Example – XML Configuration Project

  • Java 8
  • Maven 3.6.0
  • Spring 5.2.0.RELEASE
  • Eclipse

1. Create a Maven Project and Add Dependency

In Eclipse, create a maven project and add the following dependency.

File : 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">





2. Spring Bean (Java Modal Class)

Create a plain Java Class which is POJO class.

File : Employee.java

package com.learnfromexamples.springcore.modal;

public class Employee {
	private String empName;

	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String toString() {
		return "Employee [empName=" + empName + "]";


3. Spring Bean Configuration File

Create a XML configuration file by specifying the bean which needs to be created and dependency which needs to be injected. This file should be created in src/main/resources folder

File : beans1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="employee" class="com.learnfromexamples.springcore.modal.Employee">
		<property name="empName" value="Sridharan N"/>



4. Main Program

Create a Java File with main method and configuration file should be loaded using ClassPathXMLApplicationContent  .  Spring Container  (i.e ApplicationContext)will take care of creating an instance and injecting the dependency. Bean reference can be retrieved either by providing Class name or by providing the name which is specified in configuration file.

File : XMLConfiguration.java

package com.learnfromexamples.springcore.XMLConfiguration;

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

import com.learnfromexamples.springcore.modal.Employee;

public class XMLConfiguration {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans1.xml");

		Employee employee = ctx.getBean(Employee.class);

		// Getting the bean using name specified in the beans1.xml
		Employee emp = (Employee) ctx.getBean("employee");

4. Output

Employee [empName=Sridharan N]
Employee [empName=Sridharan N]

5. Source Code

Get the source code from the below git url.

Spring Hello World Example + XML Configuration
Spring Hello World Example

6. Reference

Spring Hello World

Add a Comment

Your email address will not be published. Required fields are marked *