×
☰ See All Chapters

What is spring container –Types of Spring Container

Spring containers are simple java classes and interfaces. There are no big terms in spring containers. BeanFactory is the fundamental spring container and the basis on which spring’s DI is based. Spring’s modules are built on top of the core container. Spring’s core container provides the fundamental functionality of the Spring Framework. The container defines how beans are created, configured, and managed more of the nuts and bolts of Spring. We will implicitly use these classes when we configure our application.

There are many spring containers among which two are very important.

  • Bean factories defined by the org.springframework.beans.factory.BeanFactory interface are the simplest of containers, providing basic support for dependency injection (DI).  This BeanFactory is the super interface of all the spring containers. 

  • Application contexts defined by the org.springframework.context.ApplicationContext interface build on the notion of a bean factory by providing application framework services. 

These BeanFactory and ApplicationContext  are java interfaces, but in spring world these are called as containers.

BeanFactory

  • BeanFactory is an implementation of the Factory design pattern. As the name says it is a factory of Beans whose responsibility is to create and dispense beans.  

  • BeanFactory creates and dispenses many types of beans. 

  • Including creation and dispensing, bean factory knows about many objects within an application, it is able to create associations between collaborating objects as they are instantiated. Hence we need not to worry about configuring bean itself and the bean’s client. As a result, when a bean factory hands out objects, those objects are fully configured, are aware of their collaborating objects, and are ready to use. 

To initialize the BeanFactory container we have to create the object of any of the classes which implements BeanFactory interface. ApplicationContext interface extends BeanFactory interface.

ApplicationContext interface extends the BeanFactory interface. Hence when we initialize ApplicationContext container, BeanFactory Container will also initializes. Hence to initialize BeanFactory container we can also create object of class that implements ApplicationContext.

Among the many implementations of ApplicationContext are three that are commonly used:

  • ClassPathXmlApplicationContext: Loads a context definition from an XML file located in the classpath, treating context definition files as classpath resources. 

  • FileSystemXmlApplicationContext: Loads a context definition from an XML file in the file system.( will look for xml file in a specific location within the file system) 

  • XmlWebApplicationContext: Loads context definitions from an XML file contained within a web application.( will look for xml file anywhere in the classpath including JAR files). 

For the spring example given in the previous chapter, we can initialize the BeanFactory with the different implementations as given below.

FileSystemXmlApplicationContext

 

package com.java4coding;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.FileSystemXmlApplicationContext;

 

public class Test {

        public static void main(String[] args) {

                BeanFactory factory = new FileSystemXmlApplicationContext("D:\\Application\\studentconfig.xml");

                Student student = (Student) factory.getBean("studentbean");

                student.displayInfo();

        }

}

 

ClassPathXmlApplicationContext

package com.java4coding;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Test {

        public static void main(String[] args) {

                BeanFactory factory = new ClassPathXmlApplicationContext("studentconfig.xml");

                Student student = (Student) factory.getBean("studentbean");

                student.displayInfo();

        }

}

ApplicationContext

ApplicationContext interface extends the BeanFactory interface.  To take full advantage of Spring framework most of the time we load beans using more advanced spring container ApplicationContext.

Below are some of the additional functionality it provides over BeanFactory:

  • Ability to work with multiple spring configuration files. If spring project is involved in multiple modules of spring then we need to deal with multiple spring configuration files, in that situation ApplicationContext is useful. 

  • Pre instantiation of spring beans. 

  • Support to read bean property values from properties file. 

  • Application contexts provide a means for resolving text messages, including support for internationalization (I18N) of those messages. 

  • Application contexts provide a generic way to load file resources, such as images. 

  • Application contexts can publish events to beans that are registered as listeners. 

  • We can stop ApplicationContext server by calling close() method , but BeanFactory container cannot be stopped explicitly. 

Because of the additional functionality it provides, an ApplicationContext is preferred over a BeanFactory in nearly all applications.

 


All Chapters
Author