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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.learnfromexamples.springcore</groupId>
	<artifactId>springcore-XMLConfiguration</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

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

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

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;
	}

	@Override
	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"/>
	</bean>

</beans> 

 

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) {

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

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

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


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

Leave a comment

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