Hello World Program in Spring Using XML Configuration with VS Code

SPRING CORE • XML CONFIGURATION • VS CODE

Hello World Program in Spring Using XML Configuration

Learn Spring Core XML Configuration using Java 17+, Maven, VS Code, ApplicationContext and applicationContext.xml.

🎯 What will you build?

A simple Spring Core application using XML-based configuration.

The application will display:

Hello World from Spring XML Configuration!
🧰 Technologies Used
  • Java 17+
  • Spring Framework
  • Maven
  • Visual Studio Code
  • ApplicationContext
  • applicationContext.xml
STEP 1

Project Structure

Create the following Maven project:

spring-xml-hello/ │ ├── pom.xml │ └── src/ └── main/ ├── java/ │ └── com/example/ │ ├── HelloWorld.java │ └── MainApp.java │ └── resources/ └── applicationContext.xml
📁 Important Folder
src/main/resources contains the XML configuration file that Spring loads at runtime.
STEP 2

Create Maven Project in VS Code

Open the integrated VS Code terminal and execute:

Windows CMD / VS Code Terminal
mvn archetype:generate ^
-DgroupId=com.example ^
-DartifactId=spring-xml-hello ^
-DarchetypeArtifactId=maven-archetype-quickstart ^
-DarchetypeVersion=1.5 ^
-DinteractiveMode=false

Open the Project

VS Code Terminal
cd spring-xml-hello
code .
🖥️ Linux / macOS
On Linux/macOS, use \ instead of ^ for multiline commands, or execute the Maven command on a single line.
STEP 3

Configure pom.xml

Open the project's pom.xml file.

pom.xml
<?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>
🔑 Important Dependency
The spring-context dependency provides ApplicationContext and the infrastructure required for this XML-based Spring application.
STEP 4

Create the Java Bean

Create:

src/main/java/com/example/HelloWorld.java
HelloWorld.java
package com.example;

public class HelloWorld {

    public void sayHello() {

        System.out.println(
            "Hello World from Spring XML Configuration!"
        );

    }

}
🌱 What is this class?
HelloWorld is a normal Java class.

After it is registered in the XML configuration, Spring creates and manages its object as a Spring Bean.

STEP 5

Create XML Configuration

Create the resources directory if it does not already exist:

src/main/resources

Inside the directory, create:

applicationContext.xml
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>

The Important Part

Bean Definition
<bean id="helloWorld"
      class="com.example.HelloWorld"/>
🧠 What does this tell Spring?
Create an object of com.example.HelloWorld and manage it as a Spring bean named helloWorld.

XML Bean Attributes

<bean> Defines a Spring-managed bean.
id="helloWorld" Specifies the unique name of the bean.
class="com.example.HelloWorld" Specifies the fully qualified Java class.
STEP 6

Create the Main Class

Create:

src/main/java/com/example/MainApp.java
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) {

        // Load XML configuration
        ApplicationContext context =
                new ClassPathXmlApplicationContext(
                        "applicationContext.xml");

        // Get Spring Bean
        HelloWorld hello =
                context.getBean(
                        "helloWorld",
                        HelloWorld.class);

        // Call method
        hello.sayHello();

    }

}
STEP 7

Understand the Program

The complete execution flow is:

MainApp.java
ApplicationContext
applicationContext.xml
<bean id="helloWorld" class="com.example.HelloWorld"/>
Spring creates HelloWorld object
context.getBean()
HelloWorld object
sayHello()
Hello World from Spring XML Configuration!
STEP 8

Run the Application

Compile the Project

VS Code Terminal
mvn clean compile
✅ Expected Compilation Result
BUILD SUCCESS

Run the Application

VS Code Terminal
mvn exec:java -Dexec.mainClass="com.example.MainApp"

Expected Output

Hello World from Spring XML Configuration!
STEP 9

Important Line-by-Line Explanation

Creating the Spring Container

Java
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:

src/main/resources/applicationContext.xml

Spring reads the <bean> definition and creates the required object.

Getting the Bean

Java
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

Java
hello.sayHello();

The method executes on the Spring-managed HelloWorld object.

Hello World from Spring XML Configuration!
STEP 10

Without Spring vs With Spring

❌ Traditional Java

Without Spring, the programmer directly creates the object.

HelloWorld hello =
    new HelloWorld();

hello.sayHello();
Programmer controls object creation.

✅ 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();
Spring controls object creation and management.
🧠 IoC — Inversion of Control
The basic idea is that responsibility for creating and managing the application object is transferred from the application code to the Spring IoC container.
STEP 11

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.

applicationContext.xml
<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.
📌 XML Configuration in One Sentence
applicationContext.xml tells Spring what objects should be managed and how they should be configured.
STEP 12

Final Project

spring-xml-hello/ │ ├── pom.xml │ └── src/ └── main/ ├── java/ │ └── com/example/ │ ├── HelloWorld.java │ └── MainApp.java │ └── resources/ └── applicationContext.xml

HelloWorld.java

HelloWorld.java
package com.example;

public class HelloWorld {

    public void sayHello() {

        System.out.println(
            "Hello World from Spring XML Configuration!"
        );

    }

}

applicationContext.xml

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

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

Hello World from Spring XML Configuration!
🎓 Key Takeaway

applicationContext.xml contains the Spring configuration.

ApplicationContext loads that configuration.

Spring creates and manages the HelloWorld bean.

getBean() retrieves the managed object.

Therefore:

applicationContext.xml
ApplicationContext
Spring IoC Container
HelloWorld Bean
context.getBean()

This is the fundamental concept of IoC — Inversion of Control.

Quick Recap

1. Maven Creates the Java project and manages dependencies.
2. Java Bean HelloWorld is the class managed by Spring.
3. XML Configuration applicationContext.xml defines the Spring bean.
4. ApplicationContext Loads the XML configuration and manages beans.
5. getBean() Retrieves the Spring-managed object.
6. IoC Spring takes control of object creation and management.
Spring Framework Practical Tutorial
Hello World Using XML Configuration • VS Code • Maven • ApplicationContext

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form