In order to instantiate the Spring core container, we can we create an object of the BeanFactory or ApplicationContext implementation classes by passing the Spring configuration.

The Spring Framework comes with two distinct types of containers:

  1. BeanFactory Interface
  2. ApplicationContext Interface

BeanFactory:

BeanFactory is an interface, which is coming from the org.springframework.beans.factory package. This is a root interface for accessing the spring bean container. The name itself suggested that the BeanFactory is an implementation of the Factory design pattern.

The BeanFactory interface is implemented by objects that hold a number of bean definitions, and each bean is uniquely identified by the name or id. Based on the bean definition the factory will create and return the bean instances.

The BeanFactory interface provides the basic endpoint for the spring core container towards the applications can access the core container services.

There are several implementations of BeanFactory interface. But the most useful one is org.springframework.context.support.ClassPathXmlApplicationContext.

 The syntax for using the BeanFactory to instantiate the spring core container.
BeanFacrory bf = new ClassPathXmlApplicationContext(“Spring-Bean.xml”);

Example for using Spring BeanFactory :

In the below example, we are going to implement the Spring application step by step, using maven.

Step 1 :

Create pom.xml

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.onlinetutorialspoint</groupId>
   <artifactId>SpringCoreExmples</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>SpringCoreExmples</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <!-- Spring framework -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring</artifactId>
         <version>2.5.6</version>
      </dependency>
   </dependencies>
</project>

Step 2 :

Create Employee class :

Employee.java
Employee.java
package com.onlinetutorialspoint.springcoreapplication;

public class Employee {

    private int employeeId;
    private String employeeName;

    public Employee(int employeeId, String employeeName) {
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

}
 Step 3 :
Create spring configuration file :
Spring-Bean.xml
<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-2.5.xsd">
    <bean id="employee" class="com.onlinetutorialspoint.springcoreapplication.Employee">
         <constructor-arg value="200" />
         <constructor-arg value="chandrashekhar" />
    </bean>
</beans>
 Step 4 :
Create a main class to access the Spring core context.
Client.java
package com.onlinetutorialspoint.springcoreapplication;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("Spring-Bean.xml");
        Employee employee = (Employee) beanFactory.getBean("employee");
        System.out.println("employee Id : " + employee.getEmployeeId());
        System.out.println("employee Name : " + employee.getEmployeeName());
    }
}

On the above example, by creating the ClassPathXmlApplicationContext object we obtain the BeanFactory. By using the getBean() method available in the BeanFactory, we can get the actual bean by passing the bean name, which is already defined in the Spring-Bean.xml.

Done!

Happy Learning 🙂

Download Example

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

One Comment

  1. prasanna October 24, 2018 at 4:18 pm - Reply

    well written easy to understand

Leave A Comment