How do I create my first Spring bean using XML configuration?

Creating your first Spring bean using XML configuration is a straightforward process. Here’s a step-by-step guide:


1. Add Spring Framework to Your Project

Make sure you have Spring dependencies added to your project. If you’re using Maven, include the following dependencies in your pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.2.6</version> <!-- Update to a stable version -->
</dependency>

2. Create the Bean Class

Create a simple Java class that will serve as your Spring bean. For example:

HelloWorld.java

package com.example;

public class HelloWorld {
    private String message;

    public void setMessage(String message) { // Setter method for dependency injection
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Your Message: " + message);
    }
}

3. Create the Spring XML Configuration File

Define the bean in an XML configuration file. Commonly, the file is named 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 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Definition of the HelloWorld bean -->
    <bean id="helloWorld" class="org.kodejava.spring.HelloWorld">
        <property name="message" value="Hello, Spring!"/>
    </bean>

</beans>

Here’s what’s happening:

  • id="helloWorld" specifies the name of the bean.
  • class="org.kodejava.spring.HelloWorld" points to the bean’s class.
  • The <property> tag is used to inject the value for the message property of the HelloWorld class.

4. Create the Main Class to Load the Bean

Write a Main class to load the Spring context and retrieve the bean:

MainApp.java

package org.kodejava.spring;

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

public class MainApp {
    public static void main(String[] args) {
        // Load the Spring configuration file
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");

        // Retrieve the bean from the Spring container
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

        // Call bean method
        helloWorld.getMessage();
    }
}

5. Run the Application

When you run the MainApp class, you should see the output:

Your Message: Hello, Spring!

Key Points to Remember:

  • XML-based configuration is one of the older ways to configure Spring beans and is still supported, but newer versions prefer Java-based or annotation-based configuration.
  • Ensure the applicationContext.xml file is in the classpath (e.g., under src/main/resources).

That’s it! You’ve successfully created your first Spring bean using XML configuration.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.