Spring Container

    0 Votes

Spring container is the heart of Spring. All the magic is done by this container. The container is responsible for creating Objects, Wire the Objects between themselves, manage Objects Life Cycle from creation to destruction. This container is defined by org.springframework.beans.factory.BeanFactory

Sometimes we call it as IOC container since the Dependency injection is done by Container. To create a Spring bean and to manage the wiring between them, Spring container needs instruction or Metadata. We can provide metadata in two ways.

  • By XML (What we have seen in earlier).
  • By Annotation (We will see it).
Following diagram will clear the Idea.

spring container
Spring provides two types of Containers
  • Spring BeanFactory.
  • Spring ApplicationContext.

Spring BeanFactory

The simplest container Spring provides. This container offers minimal support for Dependency Injection, some Aware interfaces, initialization and destroys hook, so that we can do additional works upon bean initialization and destruction.

It is a very simple container and is not suited for J2EE application but where we only need DI feature and some Aware feature we can use it. In Android applications, we can use Spring BeanFactory.

Let's look at an example to understand “How to create our Same HelloWorld using BeanFactory”

Beans file is same as previous

<?xml version="1.0" encoding="UTF-8"?>
<br/<beans xmlns="http://www.springframework.org/schema/beans"
<bean id="helloWorld" class="com.example.hello.HelloWorld">
<property name="greet" value="Hello World! Welcome to Spring"/>

Now we see the How to loads a bean using BeanFactory?

package com.example.hello;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class HelloWorld {
    private String greet;
    public String getGreet() {
        return greet;
    public void setGreet(String greet) {
        this.greet = greet;
    public static void main(String[] args) {
    	BeanFactory ctx = new XmlBeanFactory(new ClassPathResource("configFiles/beans.xml"));

        HelloWorld bean =(HelloWorld) ctx.getBean("helloWorld");

Please note that, here we create a XmlBeanfactory class which is a subclass of BeanFactory and pass an Implementation of Resource interface. The ClasspathResource will find the configuration file in the classpath. There is a number of implementations of Resource interface. So, a number of different ways you can search spring configuration file

Resource Implementation TypeDescription
ClassPathResource ClassPathResource, applying the given path relative to the path of the underlying resource of this descriptor
FileSystemResource FileSystemResource, applying the given path relative to the path of the underlying file of this resource descriptor.
UrlResource UrlResource, applying the given path relative to the path of the underlying URL of this resource descriptor.


The Application Context is an advanced container. This is built on top of Beanfactory, so that it can provide Dependency Injection, AOP, Manage Bean life cycle etc. Moreover, it supports JAVA EE application specific functionality like parse and read messages from a properties file, register spring bean for notifying application state event, publish custom events to event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.

The ApplicationContext has all features of the BeanFactory, Always use ApplicationContext over the BeanFactory.

We have seen an Example of Application in the previous Chapter. So I am not going repeat it here.

Some of the important ApplicationContext interface implementation

ApplicationContext InterfaceDescription
FileSystemXmlApplicationContext Container, Load XML from Your m/c filesystem.
ClassPathXmlApplicationContext Container, Load XML from classpath.
WebXmlApplicationContext Container, Load XML within web application use for Spring MVC.

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Spring Framework - Overview
Spring Architecture
Step by step guide for Spring configuration
Building First Spring application
Spring Bean XML File
Spring Bean - Learn with an Example
Spring Bean Scope
Spring Bean Life Cycle
Spring Bean Post Processor
Spring Inner Bean
Spring Autowiring
Spring Autowiring Types
Spring Qualifier with Autowiring
Spring Collection Injection
Spring Inheritance
Spring Event Handling
Dependency Injection
Spring AOP
Spring AOP Example