Hello All,

Welcome to Spring series. It’s my first blog of this series, So, today we are are going to discuss about basic Spring Bean, Spring Container and BeanFactory vs ApplicationContext. If you are pretty new to the spring framework then It’s really imperative to know this topic.

So, Let’s start the session.

Spring is a collection of modules which can be used individually and collectively.  Some main modules of the Spring framework are core, Context, AOP, Transaction, JDBC, ORM, web, web-MVC, Enterprise integration, and Testing.

Q. What Is a Spring Bean?
This is a very simple question. Spring beans are Java POJO classes that are managed by the Spring container. Let see the
example of Spring Bean:-

 

Q. What Is the Spring Container?
The Spring container is responsible for instantiating, configuring, and assembling the Spring beans. Here is an example
to configure our User POJO class for the IOC container:

bean.xml

<?xml version = “1.0” encoding = “UTF-8”?>
<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-3.0.xsd”>

               <bean id = “user” class = “User”>
<property name = name” value = “Vikash”/>
</bean>
</beans>

Spring provides two kinds of IOC container. That is:-

  1. BeanFactory container.
  2. ApplicationContext container.

 

The similarity between BeanFactory and ApplicationContext is both of them are java interfaces and configure using
XML configuration file.

BeanFactory vs ApplicationContext in Spring

BeanFactory

  • Beanfactory present in org.springframework.beans.factory.BeanFactory package.
  • If you want to use basic IOC and DI features then you should go for Beanfactory Container.
  • It is a lazy initializer. It means it can’t create the bean objects at the time of creating IOC container. It creates the bean objects on our demand when we will call the method of getBean() on BeanFactory object.
  • Annotation based Dependency Injection is not supported by BeanFactory.
  • The XmlBeanFactory is the implementation class of the BeanFactory interface. So, to use the BeanFactory, we need to create the instance of XmlBeanFactory class.

An example of the BeanFactory container usage:-

 

ApplicationContext

  • ApplicationContext present in org.springframework.context.ApplicationContext package.
  • If you want to use advanced IOC and DI features then you should go for ApplicationContext container.
  • It is eager initializer. It means, at the time of creating the IOC container itself it instantiates all the beans which scope is only singleton.
  • Annotation based Dependency Injection is supported by ApplicationContext supports using annotation such as @PreDestroy, @Autowired
  • It implements the BeanFactory interface so, We can get all the functionality of the BeanFactory using it.
  • It supports internationalization. Hence Convenient MessageSource access (for i18n) is available in it.
  • The ClassPathXmlApplicationContext class is the implementation class of the ApplicationContext interface. We need to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext.

An example of the ApplicationContext usage:-

If you want to more details about ‘BeanFactory’ and ‘ApplicationContext’ then click here.

Thanks to all.