Type1IoC就是Interface Injection,使用Type1 IoC的時(shí)候要求實(shí)現(xiàn)接口,對(duì)象所在的容器也會(huì)使用這個(gè)接口.容器同事知道這個(gè)接口上所規(guī)定的方法,所以可呼叫實(shí)現(xiàn)接口的對(duì)象來完成依賴關(guān)系注射.
如 容器的API申明了 一個(gè)IDependency接口

IDependency.java

public interface IDependency
{
public void createDependecy(Map depend);
}
接著我們讓Business 類實(shí)現(xiàn)這個(gè)接口

Business.java

public class Business implements IDependency
{
private Map dependObjects;

public void createDependency(Map dobject)
{
this.dependObjects=dobject;
//something another code
}

}
這樣如果要實(shí)現(xiàn)依賴關(guān)系注入的對(duì)象,必須實(shí)現(xiàn)IDependency接口.并將其讓容器管理,由于Business實(shí)現(xiàn)了容器所規(guī)定的接口,這樣就使得Business依賴于容器的API.如果以后要獨(dú)立出來,那么就必須修改代碼了.所以Type1IoC有較強(qiáng)的入侵性,使用它來實(shí)現(xiàn)注射.會(huì)使得應(yīng)用程序或組件依賴容器.因而降低了重用性.
所以Spring 推薦 Type2IoC 其實(shí)就是Setter Injection.寫一個(gè)Bean

HelloBean.java
package kafei.time;


public class HelloBean
{
private String helloWord;

public void setHelloWord(String helloWord)
{
this.helloWord = helloWord;
}

public String getHelloWord()
{
return helloWord;
}
}
配置Spring文件

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="helloBean"
class="kafei.time.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
下面寫一個(gè)簡單的測(cè)試文件

SpringDemo.java
package kafei.time;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;


public class SpringDemo
{

public static void main(String[] args)
{
Resource rs =
new FileSystemResource("beans.xml");
BeanFactory factory =
new XmlBeanFactory(rs);
HelloBean hello =
(kafei.time.HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
這就是一個(gè)Type2IoC,以后要改變輸出的內(nèi)容只要在配置文件中修改一下就可以了.
Type3IoC 是Construtor Injection這個(gè)Spring也支持

HelloBean.java
package kafei.time;


public class HelloBean
{
private String name;
private String helloWord;

// 建議有要無參數(shù)建構(gòu)方法

public HelloBean()
{
}

public HelloBean(String name, String helloWord)
{
this.name = name;
this.helloWord = helloWord;
}

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

public String getName()
{
return name;
}


public void setHelloWord(String helloWord)
{
this.helloWord = helloWord;
}

public String getHelloWord()
{
return helloWord;
}
}

配置spring文件

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean"
class="kafei.time.HelloBean">
<constructor-arg index="0">
<value>Justin</value>
</constructor-arg>
<constructor-arg index="1">
<value>Hello</value>
</constructor-arg>
</bean>
</beans>


有多個(gè)參數(shù)的話,用index來指定位置,第一個(gè)參數(shù)索引值為0,第二個(gè)是1,依次類推
測(cè)試文件和上面一個(gè)差不多沒有什么區(qū)別為了節(jié)省篇幅就不寫了.
這是我初學(xué)spring的一些文章.個(gè)人認(rèn)為還是Type2IoC好用,不過對(duì)于一些第一次注射后不能在修改的如DAO注射,使用Type3也可以.
上面的例子還是在spring1.28環(huán)境下的.如果要到2.0環(huán)境,請(qǐng)各位自己修改.
隔幾天寫一個(gè)關(guān)于spring 屬性參考的文章,