Note of learning Ioc Introduction.
Inversion of Control is a principle in software engineering which transfers the control of objects or portions of a program to a container or framework.
In the Spring framework, the interface ApplicationContext
represents the IoC container. The Spring container is responsible for instantiating, configuring and assembling objects known as beans
, as well as managing their life cycles.
First let’s look at this question, how to do when we want to call the method of a object inside another object?
1 | class UserService { |
We can easily think of creating an object and call it.
1 | UserDao dao = new UserDao(); |
But this way, the coupling is high. When the UserDao
changes position, the code to calling it need to change too.
An advanced method is using Factory.
We create a Factory class and implement the function ofnew UserDao()
. and then call the new method inside UserService
1 | class UserFactory { |
But the coupling is not low. we can lower it.
Using xml parsing and factory and reflection, we can achieve a low coupling way of calling the method.
First we need to create the object using xml
1 | <bean id="dao" class="xxx.xxx.UserDao"></bean> |
Then we create a factory class
1 | class UserFactory { |
Instead of return the new UserDao()
directly, we use the class declared in bean.
First we parse the xml and get the class value. Then use reflection to create the object.
So, when you change the location of your UserDao
class, you only need to change the config file, no need to modify the code.
There are two main interfaces of IOC
BeanFactory: Base realization of IOC, an inner interfaced used in Spring, not for developer. And it won’t create object when loading the config file, only when using will the object created.
ApplicationContext: sub interface of BeanFactory, will create object when loading config file. This is recommend because in web environment, we want the time wasting process of creating the object done before.
class Name | Intro |
---|---|
ClassPathXmlApplicationContext |
Creating an IOC container object by parsing the xml config file under the class path |
FileSystemXmlApplicationContext |
Creating an IOC container object by parsing the xml config file under System file path |
ConfigurableApplicationContext |
sub interface of ApplicationContext , making ApplicationContext have open, close and refresh ability. using refresh() , close() , etc. |
WebApplicationContext |
For web Application, create IOC container object base on Web environment, and store the object in ServletContext. |