

























Spring中IOC貫穿了其整個框架,但正如martinflower所說:“saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels”,IOC已經稱為框架設計中必不可少的部分。就實現上來講Spring采取了配置文件的形式來實現依賴的注射,并且支持Type2 IOC(Setter Injection)以及Type3 IOC(Constructor Injection)。
Spring中IOC的實現的核心是其Core Bean Factory,它將框架內部的組件以一定的耦合度組裝起來,并對使用它的應用提供一種面向服務的編程模式(SOP:Service-Orient Programming),比如Spring中的AOP、以及持久化(Hibernate、ibatics)的實現。
首先從最底層最基礎的factory Bean開始,先來看org.springframework.beans.factory.Bean
Factory接口,它是一個非常簡單的接口,getBean方法是其中最重要的方法,Spring通常是使用xml來populate Bean,所以比較常用的是XMLFactoryBean。
用一個簡單的示例看一下其用法。首先寫下兩個Bean類:
ExampleBean 類:







































RefBean類:









































然后可以寫個測試類來測試,當然,需要Spring中的Spring-core.jar以及commons-logging.jar,當然在elipse中可以通過安裝spring-ide插件來輕松實現。






















