HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Core / Spring – IoC Containers

Spring – IoC Containers

The Spring IoC container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application.

Spring provides following two types of containers.

  1. BeanFactory container
  2. ApplicationContext container

Read more: Inversion of Control and Dependency Injection

1. BeanFactory

A BeanFactory is essentially nothing more than the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies.

The BeanFactory enables us to read bean definitions and access them using the bean factory.

1.1. How to create BeanFactory

When using just the BeanFactory we can create one and read in some bean definitions in the XML format as follows:

InputStream is = new FileInputStream("beans.xml");
BeanFactory factory = new XmlBeanFactory(is);

//Get bean
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");

Other ways to create bean factory are as below:

Resource resource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);

ClassPathResource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);

Basically that’s all there is. Using getBean(String), you can retrieve instances of your beans; the client-side view of the BeanFactory is surprisingly simple.

1.2. BeanFactory methods

The BeanFactory interface has only six methods for client code to call:

  1. boolean containsBean(String): returns true if the BeanFactory contains a bean definition or bean instance that matches the given name
  2. Object getBean(String): returns an instance of the bean registered under the given name. Depending on how the bean was configured by the BeanFactory configuration, either a singleton and thus shared instance or a newly created bean will be returned. A BeansException will be thrown when either the bean could not be found (in which case it’ll be a NoSuchBeanDefinitionException), or an exception occurred while instantiating and preparing the bean
  3. Object getBean(String, Class): returns a bean, registered under the given name. The bean returned will be cast to the given Class. If the bean could not be cast, corresponding exceptions will be thrown (BeanNotOfRequiredTypeException). Furthermore, all rules of the getBean(String) method apply (see above)
  4. Class getType(String name): returns the Class of the bean with the given name. If no bean corresponding to the given name could be found, a NoSuchBeanDefinitionException will be thrown
  5. boolean isSingleton(String): determines whether or not the bean definition or bean instance registered under the given name is a singleton. If no bean corresponding to the given name could be found, a NoSuchBeanDefinitionException will be thrown
  6. String[] getAliases(String): Return the aliases for the given bean name, if any were defined in the bean definition

2. ApplicationContext

ApplicationContext container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.

The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory. BeanFactory can still be used for lightweight applications like mobile devices or applet based applications where data volume and speed is significant.

2.1. Types of ApplicationContext

The most commonly used ApplicationContext implementations are:

  1. FileSystemXmlApplicationContext – This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
  2. ClassPathXmlApplicationContext – This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
  3. WebXmlApplicationContext – This container loads the XML file with definitions of all beans from within a web application.

2.2. How to create ApplicationContext

A sample code for application context instantiation will look like this.

ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

Drop me your questions in comments section.

Happy Leaning !!

References:

BeanFactory Java Doc
ApplicationContext Java Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Akshat Srivastava

    October 9, 2019

    Can you add beans.xml file screenshot ? It will make things more clear.

  2. Suraj

    July 13, 2019

    Please add a next button at the end of the topic.

  3. Daniel

    May 5, 2019

    + AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext?
    Nice article 😉

  4. Sabrina

    April 2, 2019

    XmlBeanFactory is deprecated from Spring 3.1 in favor of DefaultListableBeanFactory and XmlBeanDefinitionReader

  5. Ganga

    July 20, 2018

    FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.

    but your example you mentioned simply xml file name FileSystemXmlApplicationContext constructor

  6. Bhargav

    June 10, 2017

    one question is single spring web application have multiple container ? if yes can you please post configuration and how its work. I read below from spring website but make me confused “The scope of the Spring singleton is best described as per container and per bean”

    Spring Website : Spring’s concept of a singleton bean differs from the Singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF Singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container creates one and only one instance of the class defined by that bean definition. The singleton scope is the default scope in Spring. To define a bean as a singleton in XML.

    • uday

      April 16, 2019

      Question: “The scope of the Spring singleton is best described as per container and per bean”

      Answer: “bean per container” means only one bean in a container–it doesn’t mean many containers.

  7. abhishyam

    May 21, 2016

    Nice explanation

  8. tofek khan

    February 8, 2016

    Nice explanation. Thanks

Comments are closed on this article!

Search Tutorials

Spring Tutorial

  • Spring – Introduction
  • Spring – IoC Containers
  • Spring – IoC vs. DI
  • Spring – Bean Scopes
  • Spring – Bean Life Cycle
  • Spring – Bean Postprocessors
  • Spring – Autowiring
  • Spring – Annotations
  • Spring – Stereotype Annotations
  • Spring – Task Scheduling
  • Spring – Timer Task
  • Spring – Events
  • Spring – Message Source
  • Spring – ResourceLoader
  • Spring – Property Editor
  • Spring – Send Email
  • Spring – Version-less Schema
  • Spring – Interview Questions
  • Spring – Best Practices

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)