Spring BeanFactory Interface Example

In this article, we will discuss the Spring BeanFactory interface with an example.

The BeanFactory interface provides a simple yet flexible configuration mechanism to manage objects of any nature via the Spring IoC container. Let’s have a look at some basics before diving deep into an example.

What is a Spring Bean?

Spring beans are the Java objects that form the backbone of a Spring application and are managed by the Spring IoC container. Other than being managed by the container, there is nothing special about a bean (in all other respects it’s one of many objects in the application).

What is the Spring IoC Container?

The Spring IoC container is responsible for instantiating, configuring, and assembling the beans. The container gets its information on what objects to instantiate, configure, and manage by reading configuration metadata we define for the application.

What is the BeanFactory?

BeanFactory holds bean definitions and instantiates them whenever asked for by the client application.

Spring BeanFactory Example

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

Spring Development Process

Follow these steps to develop a Spring application:

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

1. Create a Simple Maven Project

Create a simple Maven project using your favourite IDE. If you are new to Maven, read this article: How to Create a Simple Maven Project.

2. Add Maven Dependencies

Add the necessary Spring dependencies to your 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-bean-factory-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>

3. Configure HelloWorld Spring Beans

Define Spring Bean - HelloWorld.java

Spring beans are Java objects that are managed by the Spring container.

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);
    }
}

Configuration Metadata - Configure HelloWorld Spring Beans

Create an applicationContext.xml file in the src/main/resources directory:

<?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-3.0.xsd">

    <bean id="helloWorld" class="net.javaguides.spring.ioc.HelloWorld">
        <property name="message" value="Hello World!"/>
    </bean>
</beans>

4. Create a Spring Container

If we have a Spring bean configuration XML file in a standalone application, we can use ClassPathXmlApplicationContext class to load the file and get the container object.

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

5. Retrieve Beans from the Spring Container

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

The source code for this example is available on my GitHub repository: https://github.com/RameshMF/spring-core-tutorial.

This comprehensive guide has walked you through the process of setting up a simple Spring application using the BeanFactory interface, XML configuration, and retrieving beans from the Spring IoC container.

Comments