First we will understand what is IOC- Inversion of Control, The concept of IOC deals with Object Creation, Dependency Satisfaction & Life Cycle Management. The concept states that application programmers must only be concern with the use of Objects, they shouldn’t be concern with their creation and life cycle management. Application programmers should delegate these responsibilities to a Runtime Environment called IOC Container. Conventionally application programmers waste more than 50% of their efforts in object creation & their management as we can describe this situation by the following example.
Let's there be two classes named A & B. Class A has a dependency on Class B i.e. an Object of Class B is required by an Object of Class A to Perform its task.
The conventional approach does this thing as.
1. Application need an Object of Class A.
2. Object of Class A is created let ObjA.
3. Object of B class is created let ObjB.
4. Reference of ObjB is provided to ObjA.
5. ObjA is used.
Now IOC do the needful background work and it imposes the following solution to this problem.
1. Application request Object of Class A to IOC Container.
2. IOC Container Creates Object of Class A let ObjA.
3. IOC Container Creates Object of Class B let ObjB.
4. IOC Container pass the Reference of ObjB to ObjA.
5. Reference of ObjA is return back to application.
Now this will change the whole picture if Class A is depending on huge number of classes then all the work of Object creation and management will be done by IOC Container only.
Major Advantages of IOC (Inversion of Control Approach)
1. Productivity of the programmer is increased as they are responsible for the use of Object.
2. Reusability of the objects is increased because Objects are managed by Centralize party i.e.
IOC Container.
3. Maintenance of the application is simplifying because programmatic dependency of objects
is converted into declarative dependencies i.e. Dependency of Objects are expressed via a
Configuration file.
Read Here : Terminology and Components of Spring IOC
Example:
Let’s understand the whole scenario with an example. What we require.
1. An interface to provide the specification to the classes.
2. Two classes (At least) to show the dependency and relationship and conversion.
3. A user class to consume the objects.
4. Bean factory class to create the bean factory object.
5. Configuration file to provide the information to the bean factory class
Application Example:
1. Number.java
package com.jubilation.spring.user;
public interface Number {
public Number add(Number n);
public void display();
}
2. Complex.java
package com.jubilation.spring.user;
public class Complex implements Number {
private int real, img;
public Complex(int real, int img) {
this.real = real;
this.img = img;
}
@Override
public Number add(Number n) {
Complex c = (Complex) n;
int real = this.real + c.real;
int img = this.img + c.img;
return new Complex(real, img);
}
@Override
public void display() {
System.out.println("The Number is :" + real + "+" + img + "i");
}
}
3. Rational.java
package com.jubilation.spring.user;
public class Rational implements Number{
private int p, q;
public Rational(int p, int q) {
this.p = p;
this.q = q;
}
@Override
public Number add(Number n) {
Rational r = (Rational) n;
int p= this.p*r.q + this.q*r.p;
int q = this.p* r.q;
return new Rational(p, q);
}
@Override
public void display() {
System.out.println("The Number is :" + p + "/" + q);
}
}
4. IOCUser.java
package com.jubilation.spring.user;
import org.springframework.beans.factory.BeanFactory;
public class IOCUser {
public static void main(String[] args) {
// TODO code application logic here
BeanFactory factory = MyFactory.getBeanFactory();
Number n1 = (Number) factory.getBean("Complex1");
Number n2 = (Number) factory.getBean("Complex2");
Number n3 = n1.add(n2);
n3.display();
Number N1 = (Number) factory.getBean("Rational1");
Number N2 = (Number) factory.getBean("Rational2");
Number N3 = N1.add(N2);
N3.display();
}
}
5. MyFactory.java
package com.jubilation.spring.user;
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 MyFactory {
private static BeanFactory factory;
static{
Resource resource=new ClassPathResource("beanscfg.xml");
factory=new XmlBeanFactory(resource);
}
public static BeanFactory getBeanFactory() {
return factory;
}
}
6. Beanscfg.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="Complex1" class="com.jubilation.spring.user.Complex">
<constructor-arg value="34"/>
<constructor-arg value="357"/>
</bean>
<bean id="Complex2" class="com.jubilation.spring.user.Complex">
<constructor-arg value="332"/>
<constructor-arg value="133"/>
</bean>
<bean id="Rational1" class="com.jubilation.spring.user.Rational">
<constructor-arg value="34"/>
<constructor-arg value="357"/>
</bean>
<bean id="Rational2" class="com.jubilation.spring.user.Rational">
<constructor-arg value="332"/>
<constructor-arg value="13"/>
</bean>
</beans>
Let's there be two classes named A & B. Class A has a dependency on Class B i.e. an Object of Class B is required by an Object of Class A to Perform its task.
The conventional approach does this thing as.
1. Application need an Object of Class A.
2. Object of Class A is created let ObjA.
3. Object of B class is created let ObjB.
4. Reference of ObjB is provided to ObjA.
5. ObjA is used.
Now IOC do the needful background work and it imposes the following solution to this problem.
1. Application request Object of Class A to IOC Container.
2. IOC Container Creates Object of Class A let ObjA.
3. IOC Container Creates Object of Class B let ObjB.
4. IOC Container pass the Reference of ObjB to ObjA.
5. Reference of ObjA is return back to application.
Now this will change the whole picture if Class A is depending on huge number of classes then all the work of Object creation and management will be done by IOC Container only.
Major Advantages of IOC (Inversion of Control Approach)
1. Productivity of the programmer is increased as they are responsible for the use of Object.
2. Reusability of the objects is increased because Objects are managed by Centralize party i.e.
IOC Container.
3. Maintenance of the application is simplifying because programmatic dependency of objects
is converted into declarative dependencies i.e. Dependency of Objects are expressed via a
Configuration file.
Read Here : Terminology and Components of Spring IOC
Example:
Let’s understand the whole scenario with an example. What we require.
1. An interface to provide the specification to the classes.
2. Two classes (At least) to show the dependency and relationship and conversion.
3. A user class to consume the objects.
4. Bean factory class to create the bean factory object.
5. Configuration file to provide the information to the bean factory class
Application Example:
1. Number.java
package com.jubilation.spring.user;
public interface Number {
public Number add(Number n);
public void display();
}
2. Complex.java
package com.jubilation.spring.user;
public class Complex implements Number {
private int real, img;
public Complex(int real, int img) {
this.real = real;
this.img = img;
}
@Override
public Number add(Number n) {
Complex c = (Complex) n;
int real = this.real + c.real;
int img = this.img + c.img;
return new Complex(real, img);
}
@Override
public void display() {
System.out.println("The Number is :" + real + "+" + img + "i");
}
}
3. Rational.java
package com.jubilation.spring.user;
public class Rational implements Number{
private int p, q;
public Rational(int p, int q) {
this.p = p;
this.q = q;
}
@Override
public Number add(Number n) {
Rational r = (Rational) n;
int p= this.p*r.q + this.q*r.p;
int q = this.p* r.q;
return new Rational(p, q);
}
@Override
public void display() {
System.out.println("The Number is :" + p + "/" + q);
}
}
4. IOCUser.java
package com.jubilation.spring.user;
import org.springframework.beans.factory.BeanFactory;
public class IOCUser {
public static void main(String[] args) {
// TODO code application logic here
BeanFactory factory = MyFactory.getBeanFactory();
Number n1 = (Number) factory.getBean("Complex1");
Number n2 = (Number) factory.getBean("Complex2");
Number n3 = n1.add(n2);
n3.display();
Number N1 = (Number) factory.getBean("Rational1");
Number N2 = (Number) factory.getBean("Rational2");
Number N3 = N1.add(N2);
N3.display();
}
}
5. MyFactory.java
package com.jubilation.spring.user;
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 MyFactory {
private static BeanFactory factory;
static{
Resource resource=new ClassPathResource("beanscfg.xml");
factory=new XmlBeanFactory(resource);
}
public static BeanFactory getBeanFactory() {
return factory;
}
}
6. Beanscfg.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="Complex1" class="com.jubilation.spring.user.Complex">
<constructor-arg value="34"/>
<constructor-arg value="357"/>
</bean>
<bean id="Complex2" class="com.jubilation.spring.user.Complex">
<constructor-arg value="332"/>
<constructor-arg value="133"/>
</bean>
<bean id="Rational1" class="com.jubilation.spring.user.Rational">
<constructor-arg value="34"/>
<constructor-arg value="357"/>
</bean>
<bean id="Rational2" class="com.jubilation.spring.user.Rational">
<constructor-arg value="332"/>
<constructor-arg value="13"/>
</bean>
</beans>
Comments
Post a Comment