本篇內(nèi)容介紹了“Android中如何利用Spring實(shí)現(xiàn)依賴注入”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)是專業(yè)的芒市網(wǎng)站建設(shè)公司,芒市接單;提供做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行芒市網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
1.手工裝配依賴對(duì)象
手工裝配依賴對(duì)象,在這種方式中又有兩種編程方式
在xml配置文件中,通過(guò)在bean節(jié)點(diǎn)下配置
在java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配
依賴注入--手工裝配--XML方式
通過(guò)setter方法注入依賴
<bean>元素的< property >子元素指明了使用它們的set方法來(lái)注入??梢宰⑷肴魏螙|西,從基本類型到集合類,甚至是應(yīng)用系統(tǒng)的bean。
通過(guò)setter方法注入依賴
簡(jiǎn)單bean配置
配置bean的簡(jiǎn)單屬性,基本數(shù)據(jù)類型和String。
<beanidbeanid="personService" class="com.test.bean.impl.PersonServiceImpl"> <!-- 基本類型,string類型 --> <propertynamepropertyname="age"value="20"></property> <propertynamepropertyname="name" value="張無(wú)忌"></property> </bean>
通過(guò)setter方法注入依賴
引用其它bean
<beanidbeanid="person"class="com.test.bean.Person" /> <beanidbeanid="personService" class="com.test.bean.impl.PersonServiceImpl"> <!-- 引用類型 --> <propertynamepropertyname="person" ref="person" /> </bean>
內(nèi)部bean
<beanidbeanid="personService"class="com.test.bean.impl.PersonServiceImpl"> <!-- 內(nèi)部bean注入 --> <propertynamepropertyname="personClass"> <beanclassbeanclass="com.test.bean.PersonClass" /> </propert> </bean>
這種方式的缺點(diǎn)是你無(wú)法在其它地方重用這個(gè)personClass實(shí)例,原因是它是專門為personService而用。
裝配集合
若bean的屬性是集合類型,按如下處理:
A、裝配List和數(shù)組:
<!-- 裝配list --> <propertynamepropertyname="lists"> <list> <value>list1</value> <value>list2</value> <refbeanrefbean="person"/> </list> </property> <!--裝配數(shù)組 --> <property name="obj"> <list> <value>obj1</value> <value>obj2</value> <refbeanrefbean="person"/> </list> </property>
B、 裝配set:
<!--裝配set --> <property name="sets"> <set> <value>set1</value> <value>set2</value> <refbeanrefbean="person"/> </set> </property>
set使用方法和list一樣,不同的是對(duì)象被裝配到set中,而list是裝配到List或數(shù)組中裝配。
裝配集合
C、裝配map:
<!-- 裝配map--> <propertynamepropertyname="maps"> <map> <entrykeyentrykey="01"> <value>map01</value> </entry> <entrykeyentrykey="02"> <value>map02</value> </entry> </map> </property>
map中的<entry>的數(shù)值和<list>以及<set>的一樣,可以使任何有效的屬性元素,需要注意的是key值必須是String的。
D、裝配Properties:
<!--裝配Properties --> <property name="props"> <props> <prop key="01">prop1</prop> <prop key="02">prop2</prop> </props> </property>
E、設(shè)置null:
<!--裝配null --> <property name="listnull"> <null/> </property>
通過(guò)參數(shù)的順序:
<constructor-argindexconstructor-argindex="0"> <value>張三</value> </constructor-arg> <constructor-argindexconstructor-argindex="1"> <value>56</value> </constructor-arg>
通過(guò)構(gòu)造函數(shù)注入依賴
<!--通過(guò)參數(shù)的類型 --> <constructor-argtypeconstructor-argtype="java.lang.Integer"> <value>56</value> </constructor-arg> <constructor-argtypeconstructor-argtype="java.lang.String"> <value>張三</value> </constructor-arg>
依賴注入--手工裝配—注解方式
在java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配的前提條件是。
1、引入context命名空間 需要在xml配置文件中配置以下信息:
<beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>
2、在配置文件中添加context:annotation-config標(biāo)簽
<context:annotation-config/>
這個(gè)配置隱式注冊(cè)了多個(gè)對(duì)注釋進(jìn)行解析處理的處理器
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注: @Resource注解在spring安裝目錄的lib\j2ee\common-annotations.jar
在java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配,這兩個(gè)注解的區(qū)別是:@Autowired 默認(rèn)按類型裝配,@Resource默認(rèn)按名稱裝配,當(dāng)找不到與名稱匹配的bean才會(huì)按類型裝配。
@Autowired
privatePersonDao personDao;//用于字段上
@Autowired
publicvoid setPersonDao(PersonDaopersonDao) { //用于屬性的set方法上 this.personDao = personDao; }
@Autowired注解是按類型裝配依賴對(duì)象,默認(rèn)情況下它要求依賴對(duì)象必須存在,如果允許null值,可以設(shè)置它required屬性為false。
@Autowired(required=false) privatePersonDao personDao;//用于字段上 @Autowired(request=false) public voidsetPersonDao(PersonDaopersonDao) { //用于屬性的set方法上 this.personDao = personDao; }
如果我們想使用按名稱裝配,可以結(jié)合@Qualifier注解一起使用。如下:
@Autowired@Qualifier("personDao") privatePersonDao personDao;//用于字段上 @Autowired publicvoidsetPersonDao(@Qualifier("personDao") PersonDao personDao) {//用于屬性的set方法上 this.personDao= personDao; }
@Qualifier注解也能夠被指定為構(gòu)造器的參數(shù)或者方法的參數(shù):
@Resource注解和@Autowired一樣,也可以標(biāo)注在字段或?qū)傩缘膕etter方法上.
@Resource注解默認(rèn)按名稱裝配。
名稱可以通過(guò)@Resource的name屬性指定,如果沒(méi)有指定name屬性,
當(dāng)注解標(biāo)注在字段上,即默認(rèn)取字段的名稱作為bean名稱尋找依賴對(duì)象
當(dāng)注解標(biāo)注在屬性的setter方法上,即默認(rèn)取屬性名作為bean名稱尋找依賴對(duì)象。
@Resource(name="personDao") privatePersonDaopersonDao;//用于字段上 @Resource(name="personDao") publicvoidsetPersonDao(PersonDao personDao) {//用于屬性的set方法上 this.personDao = personDao; }
后一種相當(dāng)于xml配置文件中的
<propertynamepropertyname=“personDao"ref="personDao" />
注意:如果沒(méi)有指定name屬性,并且按照默認(rèn)的名稱找不到依賴對(duì)象時(shí), @Resource注解會(huì)回退到按類型裝配。但一旦指定了name屬性,就只能按名稱裝配了。
2.自動(dòng)裝配依賴對(duì)象
對(duì)于自動(dòng)裝配,大家了解一下就可以了,實(shí)在不推薦大家使用。例子:
<beanidbeanid=“foo”class=“...Foo” autowire=“autowire type”>
autowire屬性取值如下
byType:按類型裝配,可以根據(jù)屬性的類型,在容器中尋找跟該類型匹配的bean。如果發(fā)現(xiàn)多個(gè),那么將會(huì)拋出異常。如果沒(méi)有找到,即屬性值為null。
byName:按名稱裝配,可以根據(jù)屬性的名稱,在容器中尋找跟該屬性名相同的bean,如果沒(méi)有找到,即屬性值為null。
constructor與byType的方式類似,不同之處在于它應(yīng)用于構(gòu)造器參數(shù)。如果在容器中沒(méi)有找到與構(gòu)造器參數(shù)類型一致的bean,那么將會(huì)拋出異常。
autodetect :首先嘗試使用constructor來(lái)自動(dòng)裝配,然后使用byType方式。不確定性的處理與constructor方式和byType方式一致。
通過(guò)在classpath自動(dòng)掃描方式把組件納入spring容器中管理
前面的例子我們都是使用XML的bean定義來(lái)配置組件。在一個(gè)稍大的項(xiàng)目中,通常會(huì)有上百個(gè)組件,如果這些組件采用xml的bean定義來(lái)配置,顯然會(huì)增加配置文件的體積,查找及維護(hù)起來(lái)也不太方便。
spring2.5為我們引入了組件自動(dòng)掃描機(jī)制,它可以在類路徑底下尋找標(biāo)注了@Component、@Service、@Controller、@Repository注解的類,并把這些類納入進(jìn)spring容器中管理。它的作用和在xml文件中使用bean節(jié)點(diǎn)配置組件是一樣的。
要使用自動(dòng)掃描機(jī)制,我們需要打開(kāi)以下配置信息:
1、引入context命名空間 需要在xml配置文件中配置以下信息:
<beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scanbase-packagecontext:component-scanbase-package="cn.itcast"/> </beans>
2、在配置文件中添加context:component-scan標(biāo)簽
<context:component-scanbase-packagecontext:component-scanbase-package="cn.itcast"/>
其中base-package為需要掃描的包(含子包)。
注:
1、在使用組件掃描元素時(shí),AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor會(huì)隱式地被包括進(jìn)來(lái)。 也就是說(shuō),連個(gè)組件都會(huì)被自動(dòng)檢測(cè)并織入 - 所有這一切都不需要在XML中提供任何bean配置元數(shù)據(jù)。
2、功能介紹
@Service用于標(biāo)注業(yè)務(wù)層組件、
@Controller用于標(biāo)注控制層組件(如struts中的action)、
@Repository用于標(biāo)注數(shù)據(jù)訪問(wèn)組件,即DAO組件。
而@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。
//Dao層 importorg.springframework.stereotype.Repository; importcom.test.dao.PersonDao; @Repository("personDao") publicclassPersonDaoBean implements PersonDao { }
//業(yè)務(wù)層 importjavax.annotation.Resource; importorg.springframework.stereotype.Service; importcom.test.dao.PersonDao; importcom.test.service.PersonService; @Service("personService") publicclassPersonServiceBean implements PersonService { @Resource(name="personDao") privatePersonDao personDao; }
“Android中如何利用Spring實(shí)現(xiàn)依賴注入”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站題目:Android中如何利用Spring實(shí)現(xiàn)依賴注入
分享鏈接:http://aaarwkj.com/article28/gopojp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、網(wǎng)站收錄、用戶體驗(yàn)、網(wǎng)站建設(shè)、商城網(wǎng)站、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)