The basic concept of IoC

IoC (Inversion of Control)

Tradionally The java grammer usually use the ‘new’ keyword when you want to make the new object something. but It’s really different in The Spring Framework. If you want to create new instance then The Spring Framework always recommands to use the Spring Container or IoC. (Actually they are same thing.) and This coding style named as ‘IoC’. It means Inversion of Control. Actually It includes many meaningful concepts but the most important thinking is that we don’t need to make the new instance at the inner code anymore. So It’s Inversion of Control.

POJO (Plain Old Java Object)

Do you know the correct definition of POJO Class? actually I don’t know either. but I can understand just little bit about this. It’s the really pure java code style without any annotation or somthing make more complexible. The below is simple example for understaing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class Hello {
String name;
Printer printer;

public String sayHello() {
return "Hello " + name;
}

public void print() {
this.printer.print(sayHello());
}

public void setName(String name) {
this.name = name;
}

public void setPringter(Printer printer) {
this.printer = printer;
}
}

BeanDefinition

Many Spring Project existed over the world usually use the ‘xml’ file for setiing the meta information for Spring Container. So We can make some misconception of SpringFramework. It’s ‘Spring Container Setiing Information must be made by xml files’ but It’s not true.

Spring Framework has the BeanDefinition Interface. Fundamentally any type of files for setting the Information of Spring Container can be converted to this interface using BeanDefinitionReader Interface. and Actually The type of Spring Container setting information is always BeanDefinition.

1
2
Meta Resource file(.xml) > BeanDefinitionReader > BeanDefinition

Example of IoC for Understanding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.StaticApplicationContext;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.*;


public class Main {
public static void main(String[] args) {
StaticApplicationContext ac = new StaticApplicationContext(); // create Spring Container
ac.registerSingleton("hello1", Hello.class); // register the Hello POJO Class in Spring Container as the method of Singleton
// create the bean using the setting information offered StaticApplicationContext basically without BeanDefinition

Hello hello1 = ac.getBean("hello1", Hello.class); // get the Hello instance using the Spring Container
assertThat(hello1, is(notNullValue())); // It like one of the unit test, It is for checking whether the hello1 object is taken an instance from Spring Container or not


BeanDefinition helloDef = new RootBeanDefinition(Hello.class); // proclaim the BeanDefinition for controlling the setting information of IoC Container directly
helloDef.getPropertyValues().addPropertyValue("name", "Spring"); // add the attributes
ac.registerBeanDefinition("hello2", helloDef); // create the object using the BeanDefinition meta information and StaticApplicationContext BeanDefinition

Hello hello2 = ac.getBean("hello2", Hello.class);
assertThat(hello2.sayHello(), is("Hello Spring"));

assertThat(hello1, is(not(hello2)));

assertThat(ac.getBeanFactory().getBeanDefinitionCount(), is(2));
}
}


class Hello {
String name;
Printer printer;

public String sayHello() {
return "Hello " + name;
}

public void print() {
this.printer.print(sayHello());
}

public void setName(String name) {
this.name = name;
}

public void setPringter(Printer printer) {
this.printer = printer;
}
}


interface Printer {
void print(String message);
}

class StringPrinter implements Printer {
private StringBuffer buffer = new StringBuffer();

public void print(String message) {
this.buffer.append(message);
}

public String toString() {
return this.buffer.toString();
}
}

class ConsolePrinter implements Printer {
public void print(String message) {
System.out.println(message);
}
}

Share