Inversion Of Control inverts the control of your program with the purpose of decoupling. This is usually accomplished through dependency injection, a design pattern in which the container is in charge of plugging the dependencies at runtime.
In Spring the dependencies are wired through an xml configuration file or through annotations.
There are 2 types of DI in Spring:
In setter dependency injection a dependency is injected by calling setter methods. This is an example of setter DI configuration:
<bean id="zipper" class="org.davis.Zipper"> <property name="extension" value="rar" /> </bean>
In constructor dependency injection a dependency is injected through the constructor. This is an example of constructor DI configuration:
<bean id="zipper" class="org.davis.Zipper"> <constructor-arg index="0" type="java.lang.String" value="zip"/> <constructor-arg index="1" type="int" value="2"/> </bean>
Note that when we call ApplicationContext.getBean() we are not using IOC or DI, because it's not Spring to work out the bean.
Zipper zipper = (Zipper)ctx.getBean("zipper");
However, the member variables of "zipper" might be injected.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.