Nearby lessons

3 of 35

Spring - IoC Container (BeanFactory)

📌 What You Will Learn
  • Understand Spring - IoC Container (BeanFactory)
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - IoC Container (BeanFactory) step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

6) Prepare COnfiguration File

a) Right Click on "com.durgasoft.cfgs" under Source Packages.

b) Click on "New".

c) Select "Other".

d) Select "Other" under Catagories.

e) Select "SpringXMLConfig(-)" under Types.

f) Click on "Next" button.

g) Provide file name[spring_beans_config.xml].

h) Click on "Next" button.

i) Select required "Namespaces" .

j) Click on "Finish" button.

k) Provide beans configuration in the generated xml file.

spring_beans_config.xml

<beans>

<bean id="user" class="com.durgasoft.beans.User">

<property name="uname" value="Durga"/>

<property name="uqual" value="MTech"/>

<property name="uage" value="28"/>

<property name="uaddr" value="Hyd"/>

<property name="uemail" value="durga@durgasoft.com"/>

<property name="umobile" value="91-9988776655"/>

</bean>

</beans>

7) Create Test class

Test class already created at the time of creating project, where provide

application logic.

Test.java

----------

package com.durgasoft.test;

import com.durgasoft.beans.User;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args)throws Exception {

ApplicationContext context=new

ClassPathXmlApplicationContext("/com/durgasoft/cfgs/spring_beans_config.xml");

User user=(User)context.getBean("user");

user.display_User_Details();

}

}

IOC CONTAINERS

The main intention of IOC Container is to read bean configurations from configuration

file , creating Bean objects and Providing bean Objects to Spring applications.

There are two types of IOC Containers in SPring framework.

  • BeanFactory
  • ApplicationContext
  • BeanFactory

-->It is the fundamental or Base container provided by SPring Framework inorder to

manage bean objects.

-->BeanFactory IOC container will provide basic functionalities to the spring framework

by creating maintaining beans objects as per the beans configuration details which we

provided in spring beans configuration file.

-->To represent BeanFactory IOC Container, SPring framework has provided an interface

in the form of

"org.springframework.beans.factory.BeanFactory".

-->For BeanFactory interface, Spring Framework has provided an implementation class in

the form of

org.springframework.beans.factory.xml.XmlBeanFactory

-->If we want to use BeanFactory IOC Container in Spring applications then we have to

use the following steps.

7) Create Test class

-->Resource is an object in SpringFramework, it able to represent all bean configuration

details which we provided in beans configurations details.

-->To represent Resource object, Spring Framework has provided a predefined interface

in the form of

"org.springframework.core.io.Resource"

-->For Resource interface, Spring Framework has provided the following implementation

classes.

  • org.springframework.core.io.ByteArrayResource:

--> It able to represent all the beans configuration details which are available in the form

of byte[].

  • org.springframework.core.io.FileSystemResource:

--> It able to get all the beans confguration details which are available in the form of a file

in our system

hard disk.

  • org.springframework.core.io.ClassPathResource:

--> It able to get all the beans configuration details which are existed at "classpath"

environment variable

refered locations.

  • org.springframework.core.io.InputStreamResource:

--> It able to get all the beans configuration which are existed in the form of InputStream.

  • org.springframework.core.io.UrlResource:

--> It able to get all the beans configuration details which are existed at a particular URL

in the network.

  • org.springframework.web.context.support.ServletContextResource:

--> It able to get all the beans configuration details which are existed in ServletContext.

--> It will be used in spring web applications.

  • org.apringframework.web.portlet.context.PortletContextResource:

--> It able to get all the beans configuration details which are existed in PortletContext.

--> It will be used in spring web applications designed on the basis of portlets.

EX:

Resource res=new ClassPathResource("beans.xml");

Example03
JCode Cell
1 
2Create Resource Object.
3Create BeanFactory object
4Get Bean and access Business Method.Resource:
5

2) Create BeanFactory Object

To create XmlBeanFactory class object we have to use the following constructor.

public XmlBeanFactory(Resource res)

EX: BeanFactory factory=new XmlBeanFactrory(res);

3) Get Bean object from BeanFactory and access business method

To get Bean object from BeanFactory we have to use the following method.

public Object getBean(String id_Name)

EX:

HelloBean bean=(HelloBean)factory.getBean("hello");

Note: BeanFactory is deprecated in Spring3.x version.

Example:

HelloBean.java

package com.durgasoft.beans;

public class HelloBean {

static {

System.out.println("Bean Loading.....");

}

public HelloBean() {

System.out.println("Bean Created....");

}

public String sayHello() {

return "Hello User";

}

}

applicationContext.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.xsd">

<bean id="helloBean" class="com.durgasoft.beans.HelloBean"/>

</beans>

Test.java

----------

package com.durgasoft.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import com.durgasoft.beans.HelloBean;

public class Test {

public static void main(String[] args) throws Exception{

XmlBeanFactory factory = new XmlBeanFactory(new

ClassPathResource("applicationContext.xml"));

HelloBean bean = (HelloBean)factory.getBean("helloBean");

System.out.println(bean.sayHello());

}

}

In the above application, when we activate BeanFactory container then BeanFactory

Container will be started and it will not create any Bean object immediately and it will

perform th following actions.

  • It will take bean configuration file name and location from Resource object.
  • It will search for the respective bean configuration file at the specified location.
  • If the respective bean configuration file is available then Beanfactory container will

load that xml file to the memory.

  • After XML file loading, BeanFactory Container will parse that xml file, that is, it will

check all the tags in XML file are provided properly or not, all attributes are available

properly or not.

  • After the XML file parsing, BeanFactory Container will read data from Beans

configuration file and stores in Resource object.

After getting BeanFactory Object, when we access getBean(-) method then BeanFactory

will perform the following actions.

  • BeanFactory will search for the respective Bean configuration in Resource object on

the basis of the provided identity.

  • If any bean configuration is identified in Resource object on the basis of the

provided identity then BeanFactory container will take the respective bean class

name and its location.

  • BeanFactory Container will search for the respective bean class at the specified

location, if it is available then BeanFactory Container will load all the bean class

bytecode to the memory.

  • BeanFactry Container will create Object for the loaded bean class and its dependent

bean objects.

  • BeanFactory Container will store the generated bean object and its dependent

objects in Container object in the form of Key-Value pairs, where keys must be the "id"

attribute values which we specified in beans configuration file and values are Bean

Objects.

📝 Key Takeaways
  • Key ideas of Spring - IoC Container (BeanFactory) explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end