Spring的各種變量注入配置文件代碼(詳見(jiàn)項(xiàng)目:spring_injection): <property name="strValue" value
= "hello"/> 注入string類型的變量 <property name="intValue"
value = "123"/> 注入int類型的變量 <property name="listValue"> <list> <value>list1</value> <value>list2</value> </list> </property> 注入list類型的變量 <property name="setValue"> <set> <value>set1</value> <value>set2</value> <value>set3</value> </set> </property> 注入set類型的變量 <property name="arrayValue"> <list> <value>array1</value> <value>array2</value> </list> </property>
注入數(shù)組類型的變量 <property name="mapValue"> <map> <entry key="k1"
value="v1"/> <entry key="k2"
value="v2"/> </map> </property> 注入map類型的變量 如何定義屬性編輯器? (1).繼承類PropertyEditorSupport,覆寫方法setAsText(); (2).將屬性編輯器注入到spring配置文件中。 如:如果有日期類型的屬性需要注入的話,則需要寫屬性編輯器,繼承PropertyEditorSupport類,覆寫相應(yīng)的方法—setAsText() Spring公共屬性注入描述方法(詳見(jiàn)項(xiàng)目:spring_common): (1).通過(guò)<bean>標(biāo)簽定義公共屬性,制定屬性abstract = true; (2).具有相同屬性的類,只需在<bean>標(biāo)簽中指定其parent屬性即可 公共屬性注入配置文件中的代碼: <bean id = "AbstractBean" abstract="true"> <property name="id"
value="20044455"/> <property name="name"
value="Jack"/> </bean> ---------------此處相當(dāng)于java中父類 <bean id = "bean2" class
= "com.bjsxt.spring.Bean2"
parent="AbstractBean"> <property name="name" value="Jack"/> -------相當(dāng)于函數(shù)覆寫了一樣 <property name="password"
value="123456"/> </bean> --------相當(dāng)于子類繼承了AbstractBean,擁有它的屬性 <bean id = "bean3" class
= "com.bjsxt.spring.Bean3"
parent="AbstractBean"/> (*)依賴對(duì)象的注入方法可以有: (1).ref屬性 (2).<ref>標(biāo)簽 (3).內(nèi)部<bean>來(lái)定義,此時(shí)無(wú)需標(biāo)注id Spring Bean的作用域(詳見(jiàn)項(xiàng)目:spring_self_injection): 作用域?qū)傩?/span>scope可以取值: (i).singleton-----表示每次調(diào)用getBean時(shí),返回相同的實(shí)例;(系統(tǒng)默認(rèn)情況下是singleton的) (ii).prototype-----表示每次調(diào)用getBean時(shí),返回不同的實(shí)例; Spring 對(duì)AOP知識(shí)(①采用annotation方式) 1、
引用相應(yīng)的spring 架包 2、 采用Aspect定義切面 3、 在Aspect類中定義Pointcut和Advice 4、 啟用Aspect對(duì)annotation的支持,并且將Aspect類和目標(biāo)對(duì)象配置到Ioc容器中。 注意:這種方式定義中,切入點(diǎn)的方法是不被執(zhí)行的,僅起到標(biāo)識(shí)作用(即Advice通過(guò)該方法名找到相應(yīng)切入點(diǎn)) Spring 對(duì)AOP知識(shí)(②采用配置文件方式)(詳見(jiàn)項(xiàng)目:spring_aop2):在配置文件中寫入相應(yīng)的aspect切面(給出具體的所引對(duì)象)、Pointcut(切入點(diǎn))(給定他的id(用于標(biāo)識(shí),便于advice找到在哪個(gè)切入點(diǎn))和表達(dá)式(操作于哪些方法))和advice(具體的放法)(必須指定它的method(即自己添加的方法,與業(yè)務(wù)無(wú)關(guān))和Pointcut-ref(指出切入點(diǎn)))。 配置文件中代碼如下: <aop:config> <aop:aspect id = "securityAspect"
ref="securityHandler"> <aop:pointcut id="allMethods"
expression="execution(* com.bjsxt.spring.UserManagerImpl.add*(..))"/> <aop:after method="checkSecurity"
pointcut-ref="allMethods"/> </aop:aspect> </aop:config> Spring對(duì)AOP的支持(詳見(jiàn)項(xiàng)目:spring_aop2): Aspect在默認(rèn)情況下不用實(shí)現(xiàn)接口,但目標(biāo)對(duì)象必須實(shí)現(xiàn)接口,如果沒(méi)有實(shí)現(xiàn)接口,則必須引入CGLIB庫(kù) 我們可以通過(guò)在advice的具體方法中添加JoinPoint參數(shù),這個(gè)值可以由spring自動(dòng)傳入,從JoinPoint中可以獲得參數(shù)值和方法名等等。(詳見(jiàn)項(xiàng)目:spring_aop3) spring注入(setXxx()方法或構(gòu)造函數(shù)法) 屬性注入即通過(guò)setXxx()方法注入Bean的屬性值或依賴對(duì)象,由于屬性注入方式具有可選擇性和靈活性高的優(yōu)點(diǎn),因此屬性注入是實(shí)際應(yīng)用中最常用的注入方式。 代碼如下: public class Foo { private String name;
public void setName(String name) {
this.name = name;
} public String toStirng(){
return "Foo Name is :" + this.name;
} } 相應(yīng)配置文件中代碼如下: <bean id="foo" class="com.tony.test.Foo">
<property name="name" value="Foo"/>
</bean> 構(gòu)造函數(shù)注入 構(gòu)造函數(shù)注入是除屬性注入之外的另一種常用注入方式,它保證一些必要的屬性在Bean實(shí)例化時(shí)就得到設(shè)置,它保證了Bean實(shí)例在實(shí)例化后就可以使用 代碼: public class Foo { private String name;
private int age; public Foo(String name,int age){
this.name = name; this.age = age; } public String toStirng(){
return "Foo Name is :" + this.name;
} } 相應(yīng)配置文件中代碼: <bean id="foo" class="com.tony.test.Foo">
<constructor-arg type="int">
<value>20</value> </constructor-arg> <constructor-arg type="java.lang.String">
<value>Foo</value>
</constructor-arg> </bean> Spring配置中<bean>的id和name屬性區(qū)別 1、在BeanFactory的配置中,<bean>是我們最常見(jiàn)的配置項(xiàng),它有兩個(gè)最常見(jiàn)的屬性,即id和name,1.id屬性命名必須滿足XML的命名規(guī)范,因?yàn)?span lang="EN-US">id其實(shí)是XML中就做了限定的??偨Y(jié)起來(lái)就相當(dāng)于一個(gè)Java變量的命名:不能以數(shù)字、符號(hào)打頭,不能有空格,如123,?ad,"ab "等都是不規(guī)范的,Spring在初始化時(shí)就會(huì)報(bào)錯(cuò),諸如:代碼 2.name屬性則沒(méi)有這些限定,你可以使用幾乎任何的名稱,如?ab,123等,但不能帶空格,如"a b","
abc",,這時(shí),雖然初始化時(shí)不會(huì)報(bào)錯(cuò),但在getBean()則會(huì)報(bào)出諸如以下的錯(cuò)誤:代碼 3.配置文件中不允許出現(xiàn)兩個(gè)id相同的<bean>,否則在初始化時(shí)即會(huì)報(bào)錯(cuò),如: 手工配置ssh事務(wù)管理器: <!-- 配置事務(wù)管理器 --> 這里創(chuàng)建了一個(gè)id為transactionManager的事務(wù)管理器,它匹配一個(gè)session工廠,<ref bean="sessionFactory"/>這個(gè)sessionFactory是指session工廠的ID。 3) 對(duì)事務(wù)管理器進(jìn)行事務(wù)設(shè)置。增加如下代碼: <tx:advice id="smAdvice"
transaction-manager="transactionManager"> 這里創(chuàng)建了一個(gè)advice(通知),對(duì)事務(wù)管理器進(jìn)行事務(wù)設(shè)置,這里意思是指,對(duì)于以save、del、update開(kāi)頭的方法應(yīng)用事務(wù)。 4) 下面就把事務(wù)應(yīng)用到具體的類??慈缦麓a: <aop:config> 這里配置的作用是把我們上面創(chuàng)建的advice應(yīng)用到具體的類中。以上代碼的意思指,給test.service.impl下的所有類的所有方法應(yīng)用smAdvice。 5) 示例:使用Session。 幾種事物的傳播特性 1. PROPAGATION_REQUIRED: 如果存在一個(gè)事務(wù),則支持當(dāng)前事務(wù)。如果沒(méi)有事務(wù)則開(kāi)啟。
|
|