1.三種實(shí)例化Bean的方式
- 使用類構(gòu)造器實(shí)例化(默認(rèn)無(wú)參數(shù))
- 使用靜態(tài)工廠方式實(shí)例化(簡(jiǎn)單工廠模式)
- 使用實(shí)例工廠方式實(shí)例化(工廠方法模式)
2.Bean的常用配置
-
id和name
1.一般情況下激挪,裝配一個(gè)Bean時(shí),通過(guò)指定一個(gè)id屬性作為Bean的名稱
2.id屬性在IOC容器中必須是唯一的
3.如果Bean的名稱中含有特殊字符似炎,就需要使用name屬性 -
class
用來(lái)設(shè)置一個(gè)類的完全路徑名稱关贵,主要作用是 IOC容器生成類的實(shí)例 -
scope
指定Bean的作用域 -
init-method
指定Bean初始化時(shí)的方法 -
destory-method
指定Bean銷毀時(shí)的方法遇骑,scope=singleton(單例模式)時(shí)有效
3.Bean的作用域(由scope屬性來(lái)指定)
-
singleton(默認(rèn)值)
在SpringIOC容器中僅存在一個(gè)Bean實(shí)例,Bean以單例的方式存在 -
prototype
每次調(diào)用getBean()時(shí)都會(huì)返回一個(gè)新的實(shí)例(一般在整合Struts的時(shí)候揖曾,需要把a(bǔ)ction配置成多例模式) -
request
每次HTTP請(qǐng)求都會(huì)創(chuàng)建一個(gè)新的Bean落萎,該作用域僅適用于WebApplicationContext環(huán)境 -
session
同一個(gè)HTTP Session共享一個(gè)Bean,不同的HTTP Session使用不同的Bean炭剪。該作用域僅適用于WebApplicationContext環(huán)境
4.Bean的生命周期
Spring初始化Bean或銷毀Bean的時(shí)候练链,有時(shí)需要做一些處理工作,因此Spring可以在創(chuàng)建或銷毀Bean時(shí)調(diào)用Bean的兩個(gè)生命周期方法
<bean id="xxx" class="......" init-method="init" destory-method="destory">
當(dāng)bean被載入到容器的時(shí)候調(diào)用init奴拦;
當(dāng)bean從容器中刪除的時(shí)候調(diào)用destory(scope=singleton時(shí)有效)
1.instantiate bean對(duì)象實(shí)例化
2.populate properties封裝屬性
3.如果Bean實(shí)現(xiàn)BeanNameAware執(zhí)行setBeanName
4.如果Bean實(shí)現(xiàn)BeanFactoryAware或者ApplicationContextAware設(shè)置工廠setBeanFactory或者上下文對(duì)象setApplicationContext
5.如果存在類實(shí)現(xiàn)BeanPostProcessor(后處理Bean)媒鼓,執(zhí)行postProcessBeforeInitialization,可以對(duì)我們的類進(jìn)行增強(qiáng)
6.如果Bean實(shí)現(xiàn)InitializingBean執(zhí)行afterPropertiesSet
7.調(diào)用<bean init-method=“init”>指定初始化方法init如果存在類實(shí)現(xiàn)BeanPostProcessor(處理Bean)错妖,執(zhí)行postProcessAfterInitialization
8.執(zhí)行業(yè)務(wù)處理
9.如果Bean實(shí)現(xiàn)DisposableBean執(zhí)行destory
10.調(diào)用<bean destory-method=“destory”>指定銷毀方法destory
5.DI——屬性注入
對(duì)于類成員變量绿鸣,注入方式有三種:
- 構(gòu)造函數(shù)注入
- 屬性setter方法注入
-
接口注入
Spring支持前兩種
1.構(gòu)造方法的屬性注入:
- 通過(guò)構(gòu)造方法注入Bean的屬性值或依賴的對(duì)象,它保證了Bean實(shí)例在實(shí)例化后就可以使用
- 構(gòu)造器注入在<constructor-arg>元素里生成的屬性
2.set方法的屬性注入(常用):
- 使用set方法注入暂氯,在Spring配置文件中潮模,通過(guò)<property>設(shè)置注入的屬性,普通類型的值使用value痴施,對(duì)象類型的值使用ref
3.P名稱空間的屬性注入
- 使用p命名空間
- 為了簡(jiǎn)化xml文件配置擎厢,Spring從2.5開(kāi)始引入一個(gè)新的p名稱空間
- p:<屬性名>="xxx"引入常量值
<bean id="" class="" p:屬性名=""> - p:<屬性名>-ref="xxx"引用其他bean對(duì)象
<bean id="" class="" p:對(duì)象名-ref=""> - beans中需加入: xmlns:p="http://www.springframework.org/schema/p"
4.SpEL的屬性注入
- SpEL:spring expression language究流,spring表達(dá)式語(yǔ)言,對(duì)依賴注入進(jìn)行簡(jiǎn)化
- <bean id="" value="#{表達(dá)式}">
- 語(yǔ)法:#{表達(dá)式}
#{'hello'} :使用字符串
#{beanId}:使用另一個(gè)bean
#{beanId.方法名()}:使用指定名屬性动遭,并使用方法
#{T(java.lang.Math).PI}:使用靜態(tài)字段或方法
5.復(fù)雜類型的屬性注入
<bean id="" class="">
//數(shù)組類型的屬性注入
<property name="">
<list>
<value>xxx</value>
<value>xxx</value>
</list>
</property>
//List集合類型的屬性注入
<property name="">
<list>
<value>xxx</value>
<value>xxx</value>
</list>
</property>
//Set集合類型的屬性注入
<property name="">
<set>
<value>xxx</value>
<value>xxx</value>
</set>
</property>
//Map集合類型的屬性注入
<property name="">
<map>
<entry key="aaa" value="111" />
<entry key="bbb" value="222" />
<entry key="ccc" value="333" />
</map>
</property>
//Properties類型的屬性注入
<property name="">
<props>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>