自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android Spring依賴注入

移動(dòng)開發(fā) Android
Spring的核心是輕量級(jí)的容器,它實(shí)現(xiàn)了IoC容器、非侵入性的框架,并提供AOP概念的實(shí)現(xiàn)方式,提供對(duì)持久層、事務(wù)的支持,提供MVC Web框架的實(shí)現(xiàn),并對(duì)一些常用的企業(yè)服務(wù)API(Application Interface)提供一致的模型封裝,是一個(gè)全方位的應(yīng)用程序框架。

使用構(gòu)造器注入

使用屬性setter方法注入

使用Field注入(用于注解方式)

注入依賴對(duì)象可以采用手工裝配或自動(dòng)裝配,在實(shí)際應(yīng)用中建議使用手工裝配,因?yàn)樽詣?dòng)裝配會(huì)產(chǎn)生未知情況,開發(fā)人員無法預(yù)見最終的裝配結(jié)果。

1.手工裝配依賴對(duì)象

手工裝配依賴對(duì)象,在這種方式中又有兩種編程方式

在xml配置文件中,通過在bean節(jié)點(diǎn)下配置

在java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配

依賴注入--手工裝配--XML方式

通過setter方法注入依賴

<bean>元素的< property >子元素指明了使用它們的set方法來注入??梢宰⑷肴魏螙|西,從基本類型到集合類,甚至是應(yīng)用系統(tǒng)的bean。

通過setter方法注入依賴

簡(jiǎn)單bean配置

配置bean的簡(jiǎn)單屬性,基本數(shù)據(jù)類型和String。

  1. <beanidbeanid="personService" class="com.test.bean.impl.PersonServiceImpl"> 
  2. <!-- 基本類型,string類型 --> 
  3. <propertynamepropertyname="age"value="20"></property> 
  4. <propertynamepropertyname="name" value="張無忌"></property>                         
  5. </bean> 

通過setter方法注入依賴

引用其它bean

  1. <beanidbeanid="person"class="com.test.bean.Person" /> 
  2. <beanidbeanid="personService"   
  3. class="com.test.bean.impl.PersonServiceImpl"> 
  4. <!-- 引用類型 --> 
  5. <propertynamepropertyname="person" ref="person" /> 
  6. </bean> 

內(nèi)部bean

  1. <beanidbeanid="personService"class="com.test.bean.impl.PersonServiceImpl"> 
  2. <!-- 內(nèi)部bean注入 --> 
  3. <propertynamepropertyname="personClass"> 
  4. <beanclassbeanclass="com.test.bean.PersonClass" /> 
  5. </propert> 
  6. </bean> 

這種方式的缺點(diǎn)是你無法在其它地方重用這個(gè)personClass實(shí)例,原因是它是專門為personService而用。

裝配集合

若bean的屬性是集合類型,按如下處理:

A、裝配List和數(shù)組:

  1. <!-- 裝配list --> 
  2. <propertynamepropertyname="lists"> 
  3.   <list> 
  4.     <value>list1</value> 
  5.     <value>list2</value> 
  6.     <refbeanrefbean="person"/> 
  7.   </list> 
  8. </property> 
  9. <!--裝配數(shù)組 --> 
  10. <property name="obj"> 
  11.   <list> 
  12.     <value>obj1</value> 
  13.     <value>obj2</value> 
  14.     <refbeanrefbean="person"/> 
  15.   </list> 
  16. </property> 

B、 裝配set:

  1. <!--裝配set --> 
  2. <property name="sets"> 
  3.    <set> 
  4.     <value>set1</value> 
  5.     <value>set2</value> 
  6.     <refbeanrefbean="person"/> 
  7.   </set> 
  8. </property> 

set使用方法和list一樣,不同的是對(duì)象被裝配到set中,而list是裝配到List或數(shù)組中裝配。

裝配集合

C、裝配map:

  1. <!-- 裝配map--> 
  2. <propertynamepropertyname="maps"> 
  3. <map> 
  4. <entrykeyentrykey="01"> 
  5. <value>map01</value> 
  6. </entry> 
  7. <entrykeyentrykey="02"> 
  8. <value>map02</value> 
  9. </entry> 
  10. </map> 
  11. </property> 

map中的<entry>的數(shù)值和<list>以及<set>的一樣,可以使任何有效的屬性元素,需要注意的是key值必須是String的。

D、裝配Properties:

  1. <!--裝配Properties  --> 
  2. <property name="props"> 
  3. <props> 
  4. <prop key="01">prop1</prop> 
  5. <prop key="02">prop2</prop> 
  6. </props> 
  7. </property> 

E、設(shè)置null:

  1. <!--裝配null --> 
  2. <property name="listnull"> 
  3. <null/> 
  4. </property> 

通過參數(shù)的順序:

  1. <constructor-argindexconstructor-argindex="0"> 
  2. <value>張三</value> 
  3. </constructor-arg> 
  4. <constructor-argindexconstructor-argindex="1"> 
  5. <value>56</value> 
  6. </constructor-arg> 

#p#

通過構(gòu)造函數(shù)注入依賴

  1. <!--通過參數(shù)的類型 --> 
  2. <constructor-argtypeconstructor-argtype="java.lang.Integer"> 
  3.    <value>56</value> 
  4. </constructor-arg> 
  5. <constructor-argtypeconstructor-argtype="java.lang.String"> 
  6.    <value>張三</value> 
  7. </constructor-arg> 

依賴注入--手工裝配—注解方式

在java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配的前提條件是。

1、引入context命名空間 需要在xml配置文件中配置以下信息:

  1. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans" 
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3. xmlns:context="http://www.springframework.org/schema/context" 
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6. http://www.springframework.org/schema/context  
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
  8. <context:annotation-config/> 
  9. </beans> 

2、在配置文件中添加context:annotation-config標(biāo)簽

  1. <context:annotation-config/> 

這個(gè)配置隱式注冊(cè)了多個(gè)對(duì)注釋進(jìn)行解析處理的處理器

  1. AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,  
  2.  
  3. 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

  1. privatePersonDao  personDao;//用于字段上 

@Autowired

  1. publicvoid setPersonDao(PersonDaopersonDao) { //用于屬性的set方法上  
  2.        this.personDao = personDao;  
  3. }  

@Autowired注解是按類型裝配依賴對(duì)象,默認(rèn)情況下它要求依賴對(duì)象必須存在,如果允許null值,可以設(shè)置它required屬性為false。

  1. @Autowired(required=false)  
  2.    privatePersonDao  personDao;//用于字段上  
  3. @Autowired(request=false)  
  4.    public voidsetPersonDao(PersonDaopersonDao) {  //用于屬性的set方法上  
  5.        this.personDao = personDao;  

如果我們想使用按名稱裝配,可以結(jié)合@Qualifier注解一起使用。如下:

  1. @Autowired@Qualifier("personDao")  
  2.    privatePersonDao  personDao;//用于字段上  
  3. @Autowired  
  4. publicvoidsetPersonDao(@Qualifier("personDao") PersonDao personDao) {//用于屬性的set方法上  
  5.       this.personDao= personDao;  

 @Qualifier注解也能夠被指定為構(gòu)造器的參數(shù)或者方法的參數(shù):

@Resource注解和@Autowired一樣,也可以標(biāo)注在字段或?qū)傩缘膕etter方法上.

@Resource注解默認(rèn)按名稱裝配。

名稱可以通過@Resource的name屬性指定,如果沒有指定name屬性,

當(dāng)注解標(biāo)注在字段上,即默認(rèn)取字段的名稱作為bean名稱尋找依賴對(duì)象

當(dāng)注解標(biāo)注在屬性的setter方法上,即默認(rèn)取屬性名作為bean名稱尋找依賴對(duì)象。

  1. @Resource(name="personDao")  
  2.     privatePersonDaopersonDao;//用于字段上  
  3. @Resource(name="personDao")  
  4. publicvoidsetPersonDao(PersonDao personDao) {//用于屬性的set方法上  
  5.     this.personDao = personDao;  

后一種相當(dāng)于xml配置文件中的

  1. <propertynamepropertyname=“personDao"ref="personDao" /> 

注意:如果沒有指定name屬性,并且按照默認(rèn)的名稱找不到依賴對(duì)象時(shí), @Resource注解會(huì)回退到按類型裝配。但一旦指定了name屬性,就只能按名稱裝配了。

2.自動(dòng)裝配依賴對(duì)象

對(duì)于自動(dòng)裝配,大家了解一下就可以了,實(shí)在不推薦大家使用。例子:

  1. <beanidbeanid=“foo”class=“...Foo” autowire=“autowire type”> 

autowire屬性取值如下

byType:按類型裝配,可以根據(jù)屬性的類型,在容器中尋找跟該類型匹配的bean。如果發(fā)現(xiàn)多個(gè),那么將會(huì)拋出異常。如果沒有找到,即屬性值為null。

 byName:按名稱裝配,可以根據(jù)屬性的名稱,在容器中尋找跟該屬性名相同的bean,如果沒有找到,即屬性值為null。

constructor與byType的方式類似,不同之處在于它應(yīng)用于構(gòu)造器參數(shù)。如果在容器中沒有找到與構(gòu)造器參數(shù)類型一致的bean,那么將會(huì)拋出異常。

autodetect :首先嘗試使用constructor來自動(dòng)裝配,然后使用byType方式。不確定性的處理與constructor方式和byType方式一致。

通過在classpath自動(dòng)掃描方式把組件納入spring容器中管理

前面的例子我們都是使用XML的bean定義來配置組件。在一個(gè)稍大的項(xiàng)目中,通常會(huì)有上百個(gè)組件,如果這些組件采用xml的bean定義來配置,顯然會(huì)增加配置文件的體積,查找及維護(hù)起來也不太方便。

spring2.5為我們引入了組件自動(dòng)掃描機(jī)制,它可以在類路徑底下尋找標(biāo)注了@Component、@Service、@Controller、@Repository注解的類,并把這些類納入進(jìn)spring容器中管理。它的作用和在xml文件中使用bean節(jié)點(diǎn)配置組件是一樣的。

要使用自動(dòng)掃描機(jī)制,我們需要打開以下配置信息:

1、引入context命名空間 需要在xml配置文件中配置以下信息:

 

  1. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans" 
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3. xmlns:context="http://www.springframework.org/schema/context" 
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6. http://www.springframework.org/schema/context  
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
  8. <context:component-scanbase-packagecontext:component-scanbase-package="cn.itcast"/> 
  9. </beans> 

2、在配置文件中添加context:component-scan標(biāo)簽

  1. <context:component-scanbase-packagecontext:component-scanbase-package="cn.itcast"/> 

其中base-package為需要掃描的包(含子包)。

注:

1、在使用組件掃描元素時(shí),AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor會(huì)隱式地被包括進(jìn)來。 也就是說,連個(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ù)訪問組件,即DAO組件。

而@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。

  1. //Dao層  
  2. importorg.springframework.stereotype.Repository;  
  3. importcom.test.dao.PersonDao;  
  4.   @Repository("personDao")  
  5.    publicclassPersonDaoBean implements PersonDao {  

 

  1. //業(yè)務(wù)層  
  2. importjavax.annotation.Resource;  
  3. importorg.springframework.stereotype.Service;  
  4. importcom.test.dao.PersonDao;  
  5. importcom.test.service.PersonService;  
  6.  @Service("personService")  
  7.    publicclassPersonServiceBean implements PersonService {  
  8.  @Resource(name="personDao")  
  9.    privatePersonDao personDao;  

【編輯推薦】

Android布局屬性詳解

Android Spinner實(shí)例

Android智能手機(jī)操作系統(tǒng)

Android中preference的使用

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2022-04-30 08:50:11

控制反轉(zhuǎn)Spring依賴注入

2016-03-21 17:08:54

Java Spring注解區(qū)別

2014-07-08 14:05:48

DaggerAndroid依賴

2009-09-08 15:22:20

Spring依賴注入

2020-08-06 00:14:16

Spring IoC依賴注入開發(fā)

2023-10-07 08:35:07

依賴注入Spring

2011-03-01 13:45:41

Spring3Annotation

2021-12-15 09:17:12

Spring依賴注入面試題

2022-08-24 07:06:36

SpringSetter項(xiàng)目

2023-07-11 09:14:12

Beanquarkus

2022-12-29 08:54:53

依賴注入JavaScript

2017-08-16 16:00:05

PHPcontainer依賴注入

2016-10-20 19:36:01

androiddagger2依賴注入

2021-07-29 06:55:03

Spring@AutowriedbyType注入

2019-09-18 18:12:57

前端javascriptvue.js

2015-09-02 11:22:36

JavaScript實(shí)現(xiàn)思路

2014-01-07 14:53:37

Android開發(fā)依賴注入Roboguice

2024-08-26 08:52:41

2024-12-30 12:00:00

.NET Core依賴注入屬性注入

2024-04-01 00:02:56

Go語言代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)