Springs - SPLessons

Spring IOC Containers

Chapter 24

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring IOC Containers

shape Introduction

Ioc(Inversion of Control) Container is an object. IOC container performed many operations.
1. to instantiate the application class.
2. to configure the object
3. to assemble the dependencies between the objects.


BeanFactory IOC Container
BeanFactory is supported for the Dependency Injection (DI) and defined by the org.springframework.beans.factory.BeanFactory. BeanFactory is an interface so we don’t create BeanFactory object directly, We create the BeanFactory object using implementation class is XmlBeanFactory. First Container will read the XML file through Resource object after BeanFactory object gets the Resource object.

1. Create the project directory structure.

2. Create the WelcomeBean class with the name property.

WelcomeBean.java

package com.splessons;

public class WelcomeBean
{
  
	private String name;

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}
	public void display()
	{
		System.out.println(name);
	}
	
}

3. Create the Spring xml file with name applicationContext.xml.
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"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="welcomeBeanId" class="com.splessons.WelcomeBean">
       <property name="name" value="Welcome to splessons"/>
    </bean>
 </beans>  

4. Create the BeanFactory class and reads the xml file using Resource object and BeanFactory object.
BeanFactoryClass.java

package com.splessons;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BeanFactoryClass 
{

	public static void Main(String[] args)
    {
		// TODO Auto-generated method stub
	 Resource resource=new ClassPathResource("applicationcontext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		WelcomeBean bean=(WelcomeBean)factory.getBean("welcomeBeanId");
           bean.display();
	}  
    }


ApplicationContext IOC Container
ApplicationContext is defined by org.springframework.context.ApplicationContext.
The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext. Here we need to create ApplicationContext then don’t create the Resource object. AplicationContext is directly reading the Spring xml file.
We need to instantiate the ApplicationContext object like


1. Create Project directory structure.
2. Create the WelcomeBean class with property name.
WelcomeBean.java

package com.splessons;

public class WelcomeBean
{
  
	private String name;

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}
	public void display()
	{
		System.out.println(name);
	}
	
}

3. Create the Spring xml with name 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"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="welcomeBeanId" class="com.splessons.WelcomeBean">
       <property name="name" value="Welcome to splessons"/>
    </bean>
 </beans>  

3. Create the ApplicationContext class then the container will read the spring XML file.

package com.splessons;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextClass 
{

	public static void Main(String[] args)
    {
		// TODO Auto-generated method stub
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext();
		WelcomeBean bean=(WelcomeBean)applicationContext.getBean("welcomeBeanId");
                       bean.display();
	}  
    

}



Spring – Related Information
Spring Modules

Spring Dependency Injection

Spring Auto Wiring

Spring Bean

Are you looking for a Job? Stay Updated with Limitless Notifications!!



Join us on Telegram Channel