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 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

 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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
      <!-- Spring framework -->

Step 2 :

Create Employee class :
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 :
<beans xmlns=""        xmlns:xsi=""        xsi:schemaLocation="">
    <bean id="employee" class="com.onlinetutorialspoint.springcoreapplication.Employee">
         <constructor-arg value="200" />
         <constructor-arg value="chandrashekhar" />
 Step 4 :
Create a main class to access the Spring core context.
package com.onlinetutorialspoint.springcoreapplication;

import org.springframework.beans.factory.BeanFactory;

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.


Happy Learning 🙂

Download Example