Spring IOC Container Java Config Example

In a previous article, we discussed What is Spring IOC Container and how it works, Now in this article, we will discuss a simple example to demonstrate Spring IOC Container with Java-based configuration metadata?
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. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects.

Three ways we can 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 five steps to develop a spring application:
  1. Create a simple Maven Project
  2. Project Structure
  3. Add Maven Dependencies
  4. Configure HelloWorld Spring Beans
  5. Create a Spring Container
  6. Retrieve Beans from Spring Container

Tools and technologies used

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

1. Create a simple Maven Project

Create a simple maven project using your favorite IDE and refer below diagram for the packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.

2. Project Structure

The below diagram shows a project structure for your reference -

3. Add Maven Dependencies

Add the following content to the pom.xml file:
<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>net.javaguides.spring</groupId>
	<artifactId>spring-ioc-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<maven.compiler.target>17</maven.compiler.target>
		<maven.compiler.source>17</maven.compiler.source>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>6.0.6</version>
		</dependency>
	</dependencies>
</project>

4. Configure HelloWorld Spring Beans

What is a Spring Bean?

This is a very simple question that is often overcomplicated. Usually, Spring beans are Java objects that are managed by the Spring container.

Let's 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, Let's configure the HelloWorld class as 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;
    }
}
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

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.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

6. Retrieve Beans from Spring Container

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) {
        // ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.close();
    }
}

Output

My Message : Hello World!

GitHub 

The source code of this example is available on my GitHub repository.

Comments