Hello World Program in Spring Using XML Configuration
Learn Spring Core XML Configuration using Java 17+, Maven, VS Code, ApplicationContext and applicationContext.xml.
A simple Spring Core application using XML-based configuration.
The application will display:
- Java 17+
- Spring Framework
- Maven
- Visual Studio Code
- ApplicationContext
- applicationContext.xml
Project Structure
Create the following Maven project:
src/main/resources contains the XML configuration
file that Spring loads at runtime.
Create Maven Project in VS Code
Open the integrated VS Code terminal and execute:
mvn archetype:generate ^ -DgroupId=com.example ^ -DartifactId=spring-xml-hello ^ -DarchetypeArtifactId=maven-archetype-quickstart ^ -DarchetypeVersion=1.5 ^ -DinteractiveMode=false
Open the Project
cd spring-xml-hello code .
\ instead of
^ for multiline commands, or execute the
Maven command on a single line.
Configure pom.xml
Open the project's pom.xml file.
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-xml-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
</plugin>
</plugins>
</build>
</project>
spring-context dependency provides
ApplicationContext and the infrastructure
required for this XML-based Spring application.
Create the Java Bean
Create:
package com.example;
public class HelloWorld {
public void sayHello() {
System.out.println(
"Hello World from Spring XML Configuration!"
);
}
}
After it is registered in the XML configuration, Spring creates and manages its object as a Spring Bean.
Create XML Configuration
Create the resources directory if it does not already exist:
Inside the directory, create:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld"
class="com.example.HelloWorld"/>
</beans>
The Important Part
<bean id="helloWorld"
class="com.example.HelloWorld"/>
com.example.HelloWorld and manage it as a
Spring bean named helloWorld.
XML Bean Attributes
Create the Main Class
Create:
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
// Load XML configuration
ApplicationContext context =
new ClassPathXmlApplicationContext(
"applicationContext.xml");
// Get Spring Bean
HelloWorld hello =
context.getBean(
"helloWorld",
HelloWorld.class);
// Call method
hello.sayHello();
}
}
Understand the Program
The complete execution flow is:
Run the Application
Compile the Project
mvn clean compile
BUILD SUCCESS
Run the Application
mvn exec:java -Dexec.mainClass="com.example.MainApp"
Expected Output
Important Line-by-Line Explanation
Creating the Spring Container
ApplicationContext context =
new ClassPathXmlApplicationContext(
"applicationContext.xml");
ClassPathXmlApplicationContext reads the XML
configuration file from the application's classpath.
In this project the file is located at:
Spring reads the <bean> definition and
creates the required object.
Getting the Bean
HelloWorld hello =
context.getBean(
"helloWorld",
HelloWorld.class);
The first argument:
"helloWorld"
is the bean ID defined in:
<bean id="helloWorld"
class="com.example.HelloWorld"/>
The second argument:
HelloWorld.class
tells Spring the expected type of the bean.
Calling the Method
hello.sayHello();
The method executes on the Spring-managed
HelloWorld object.
Without Spring vs With Spring
❌ Traditional Java
Without Spring, the programmer directly creates the object.
HelloWorld hello =
new HelloWorld();
hello.sayHello();
✅ Using Spring XML Configuration
Spring creates and manages the object.
ApplicationContext context =
new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld hello =
context.getBean(
"helloWorld",
HelloWorld.class);
hello.sayHello();
What Exactly Is XML Configuration?
XML configuration means that Spring's bean information is stored in an XML file instead of being declared directly through Java annotations.
<bean id="helloWorld"
class="com.example.HelloWorld"/>
This configuration tells Spring three important things:
| Element / Attribute | Meaning |
|---|---|
<bean>
|
Defines a Spring-managed bean. |
id="helloWorld"
|
Defines the name used to identify the bean. |
class="com.example.HelloWorld"
|
Specifies the fully qualified Java class that Spring should instantiate. |
applicationContext.xml tells Spring
what objects should be managed and how they
should be configured.
Final Project
HelloWorld.java
package com.example;
public class HelloWorld {
public void sayHello() {
System.out.println(
"Hello World from Spring XML Configuration!"
);
}
}
applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld"
class="com.example.HelloWorld"/>
</beans>
MainApp.java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld hello =
context.getBean(
"helloWorld",
HelloWorld.class);
hello.sayHello();
}
}
Output
applicationContext.xml contains the Spring configuration.
ApplicationContext loads that configuration.
Spring creates and manages the HelloWorld bean.
getBean() retrieves the managed object.
Therefore:
This is the fundamental concept of IoC — Inversion of Control.