Spring IOC Container Java Config Example

In this tutorial, we will demonstrate how to configure the Spring IoC (Inversion of Control) Container using Java-based configuration metadata.

Introduction

In a previous article, we discussed What is Spring IOC Container and how it works. Now, let's look at a practical example to understand how to configure Spring beans using Java-based configuration.

We will use the latest Spring release - Spring 6.0.5.

The Spring IoC container is responsible for instantiating, configuring, and assembling the Spring beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata, which can be represented in XML, Java annotations, or Java code.

Ways to Supply Configuration Metadata to the Spring IoC Container

  1. XML-based configuration
  2. Annotation-based configuration
  3. Java-based configuration

Spring IOC Container Java Config Example

In this example, we will supply Java-based configuration metadata to the Spring IoC container.

Development Steps

Follow these steps to develop a Spring application:

  1. Create a simple Maven Project
  2. Add Maven Dependencies
  3. Configure HelloWorld Spring Bean
  4. Create a Spring Container
  5. Retrieve Beans from the Spring Container

Tools and Technologies Used

  • Spring Framework - 6.0.5
  • JDK - 17 or later
  • Maven - 3.2+
  • IDE - Eclipse Mars/STS

Step 1: Create a Simple Maven Project

Create a simple Maven project using your favourite IDE and refer to the Guide to Create a Simple Maven Project.

Step 2: Project Structure

The below diagram shows a project structure for your reference:

Project Structure

Step 3: Add Maven Dependencies

Add the following content to the pom.xml file:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.1.8</version>
        </dependency>

Step 4: Configure HelloWorld Spring Bean

What is a Spring Bean?

A Spring bean is a Java object that is managed by the Spring container.

Create a HelloWorld Java class with the following content:

package net.javaguides.spring.ioc;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

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

Next, configure the HelloWorld class as a Spring bean using Java-based configuration:

package net.javaguides.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public HelloWorld helloWorld() {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setMessage("Hello World!");
        return helloWorld;
    }
}

Step 5: Create a Spring Container

Let's create a Spring container object using AnnotationConfigApplicationContext implementation class of the ApplicationContext interface:

package net.javaguides.spring.ioc;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();
    }
}

Step 6: Retrieve Beans from Spring Container

The ApplicationContext interface provides the getBean() method to retrieve the bean from the Spring container.

package net.javaguides.spring.ioc;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.close();
    }
}

Output

My Message: Hello World!

Conclusion

In this tutorial, we learned how to configure spring beans using Java-based configuration. We covered the steps to create a Maven project, add dependencies, configure beans, create a Spring container, and retrieve beans from the container.

The source code for this example is available in my GitHub repository.

Additional Resources

Comments