1.原則: Don't call us,we will call you
2.原來構(gòu)建對象,對象內(nèi)如果有其他依賴對象,需要一個個構(gòu)建后傳入,現(xiàn)在直接先注入好,構(gòu)建原對象的時候
它會直接向Ioc service Provider 去要
3.注入對象的方式有
A. 構(gòu)造方法方式注入
B.通過setter 方式注入
C.實現(xiàn)提供的接口來注入
4.Ioc service provider 有兩種功能
A. 業(yè)務(wù)對象構(gòu)建管理
B. 業(yè)務(wù)對象間依賴綁定
5.Ioc service provider 簡化代碼來構(gòu)建對象,但是這些依賴條件Ioc service provider
如何知道?
A.直接編碼方式知道
比如
IoContainer container=...
container.register(FXNewProvider.class,new FXNewsProvider());
B.配置文件方式
<bean id="newsProvider" ...>
....
</bean>
C.通過注解來注入
6.Spring Ioc 容器
Ioc service provider 是其中的一部分
Ioc 容器spring提供兩種類型
A.BeanFactory :基礎(chǔ)類型Ioc 容器 默認為延時初始化策略,啟動快需要資源有限
B.ApplicationContext:BeanFactory 基礎(chǔ)上構(gòu)建,屬于高級容器,提供事件發(fā)布,國際化等
高級功能,需要資源相對多,啟動相對也就慢。
7.bean 的生命周期
A.singleton: 在spring Ioc 容器中只存在一個實例,他的生命周期從容器啟動并因為第一次被
請求而初始化后,將一直存活到容器退出。這里的singleton 和Gof中提出的Singleton 有點區(qū)別
bean的singleton 表示在同一個容器中存在一個實例;而Gof中的Singleton表示同一個classloader中
只存在一個實例。
B.prototype:代理
容器在接收到請求后,為之生成一個新對象實例給請求方,然后就不再有新對象的引用。
C.request,session,global session
只適用web 應(yīng)用,不像上面的那么通用。通常與XmlWebApplicationContext一起用
request:每一個請求生成一個新的requestProcessor 對象,和http request的生命周期一樣
session:和http session 生命周期一個
global session:只應(yīng)用在基于portlet 的web應(yīng)用程序才有意義。映射到portlet的global
范圍的session
D.自定義scope類型
實現(xiàn)org.springframework.beans.factory.config.Scope接口
public infterface Scope{
...
}
其中g(shù)et和remove 方法必須實現(xiàn),參考例子
http://jroller.com/eu/entry/more_fun_with_spring_scopes
有了Scope的實現(xiàn)類后,需要把Scope注冊到容器中,才能提供相應(yīng)的bean定義
通過ConfigurableBeanFactory的 registerScope中進行注冊。BeanFactory
通常實現(xiàn)ConfigurableBeanFactory接口。
如果是ApplicationContext,你還可以通過配置文件來實現(xiàn),ApplicationContext
會自動識別并加載.
E.面向接口編程指以接口為導向,在類中引用接口而不是具體接口的實現(xiàn)類,這樣就減少和
特定實現(xiàn)類的耦合,比如傳統(tǒng)寫法
public class Foo{
private BarInterface barInstance;
public Foo()
{
barInstance = new BarInterfaceImp();
//BarInterfaceImp其實是BarInterface 接口的實現(xiàn)類
}
}
上面的寫法耦合很大,如果該類是由我們設(shè)計并開發(fā)的,那么還好說,我們可以通過依賴注入,
讓容器幫助我們解除接口和實現(xiàn)類之間的耦合性,但是,有時,我們需要依賴第三方庫,需要實例化
并通過第三方庫中的相關(guān)類,這時,接口和實現(xiàn)類的耦合性需要其他方式來避免。
比如工程方法模式。提供一個工廠類來實例化具體的接口實現(xiàn)類,這樣主體對象只需要依賴工廠類,具體使用的實現(xiàn)類有變革的話,只要變更工廠類。
上面的Foo可以改為:
public class Foo{
private BarInterface barInterface;
public Foo()
{
//barInterface = BarInterfaceFactory.getInstance();
或者
//barInterface =new BarInterfaceFactory().getInstance();
}
}
靜態(tài)工廠方法:
public class StaticBarInterfaceFactory
{
public static BarInterface getInstance()
{
return new BarInterfaceImpl();
}
}
spring 提供配置來支持靜態(tài)工廠方法
<bean id="bar" class="...StaticBarInterfaceFactory" factory-method="getInstance">
//如果factory-method 有參數(shù)可以,進入下面參數(shù)
<constructor-arg>
<ref bean="foobar">
</constructor-arg>
</bean>
非靜態(tài)工廠方法:
public class NoStaticBarInterfaceFactory
{
public BarInterface getInstance()
{
return new BarInterfaceImpl();
}
}
spring配置如下:
<bean id="barFactory" class="...NoStaticBarInterfaceFactory" />
<bean id="bar" factory-bean="barFactory" factory-method="getInstance" />
FactoryBean:spring 提供的生產(chǎn)對象的工廠bean
只需要實現(xiàn)org.springframework.beans.factory.FactoryBean 接口
public interface FactoryBean{
//獲取實例
Object getObject() throws Exception;
//獲取實現(xiàn)相對于的類型
Class getObjectType();
//表明獲取的實例在容器中是否是singleton形式
boolean isSingleton();
}
然后在spring 進行bean定義
<bean id="objectId" class="...FactoryBean"/>需要注意的是,這里的objectId不是
FactoryBean類型,而是上面實現(xiàn)接口的具體getObjectType的類型。
F.方法注入和方法替換。
方法替換實現(xiàn)org.springframework.beans.facotry.support.MethodReplacer
并在bean中定義
<replaced-method name="getAndPersistNews" replacer="providerReplacer"/>
<bean id="providerReplacer" class="...FXNewsReplacer">
其實方法替換就是對方法的攔截。
G.ApplicationContext特性
Resource:spring 定義統(tǒng)一的資源
ResourceLoader:有了資源,需要有加載工具。
ResourcePatternResolver:是ResourceLoader的擴展,resourceLoader每次只能根據(jù)
資源返回單個Resource實例,而ResourcePatternResolver可以一次返回多個Resource