Thursday, 22 February 2018

3.0 Inversion of Control (IoC)

IoC means - Design construction and management of the object (Object outsourcing for the construction of object)

IOC - Inversion of control
It is an architectural pattern which tells that programmer don't need to create any object instead describe how it should be created in an xml or file.
Object creation and its life cycle will be manage by spring container. This is called inversion of control.

It is a programming model use to develop objects of an application so that they can be reused and managed.

Tight coupling code is difficult for maintenance
A---->depend on ------->B
IOC is a programming model which state that resources required by object for performing its operation must not created by the object itself rather they must be provided at runtime.

To put the values in reference variable to remove the dependency - Spring use dependency injection.

Object doesn't ask for the resources rather it is provided.

Don't call me,I'll call you.

IOC is a design pattern which is implemented by DI


YOUTUBE VIDEO TUTORIAL - by AndroJava Tech4U






Primary function of the Spring container

  1. Create and manage  of object (Inversion of Control)
  2. Inject Object's dependency (Dependency Injection)

There are three ways for  Configuring Container
  1. xml configuration file
  2. Java Annotations
  3. Java Source code

Spring Development Proces

1.Creating your spring beans


File : applicationContext.xml
<beans ...>
          <bean id="myCoach">
          </bean class="com.spring.demo.BaselballCoach">
</beans>

2.Create a spring container
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");

3.Retrieve beans from spring container
Coach obj=context.getBean("myCoach", Coach.class );


Spring container is generally known as ApplicationContext


First SPRING example

























Human.java
---------------------------------------------------------------------
package com.spring.demo;

public interface Human {
public String getMyType();
}


Men.java
---------------------------------------------------------------------
package com.spring.demo;

public class Men implements Human{
public String getMyType(){
return "I am a men";
}
}

Women.java
---------------------------------------------------------------------
package com.spring.demo;

public class Women implements Human{
public String getMyType(){
return "I am a Women";
}
}


MyApp.java
---------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {
public static void main(String args[])
{
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Human type1=context.getBean("myType1",Human.class);
Human type2=context.getBean("myType2",Human.class);
System.out.println(type1.getMyType());
System.out.println(type2.getMyType());
context.close();
}
}

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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


  <bean id="myType1"
  class="com.spring.demo.Men">
  </bean>
 
  <bean id="myType2"
  class="com.spring.demo.Women">
  </bean>


</beans>

---------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------
Feb 23, 2018 10:44:53 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Fri Feb 23 10:44:53 IST 2018]; root of context hierarchy
Feb 23, 2018 10:44:53 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
I am a Women
Feb 23, 2018 10:44:53 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Fri Feb 23 10:44:53 IST 2018]; root of context hierarchy





No comments:

Post a Comment