




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
企業級JAVAII
軟件工程系:聶常紅
cred_n@163.com內容回顧:Spring概念Spring的配置文件作用、主要內容、保存位置ApplicationContextSpringBean的作用域配置文件的加載第15講依賴注入15.1依賴注入概述15.2設值注入15.3構造器注入15.4依賴關系配置15.5注解注入15.6自動掃描機制15.7用注解對Bean進行初始化和銷毀15.6手動裝配和自動裝配15.1依賴注入概述依賴注入,即IoC,指由容器創建的對象在運行期,動態地注入到應用的組件中常用的依賴注入方式:設值(setter)注入構造器注入注解注入15.2設值注入設值注入,通過屬性的Setter方法注入Bean的屬性值。優點:具有可選擇性,靈活性比較高語法:必須提供默認的構造函數必須為需要注入值的屬性提供對應的Setter方法在配置文件中必須使用<property>配置屬性的注入值packagecom.sise.service.impl;publicclassPersonServiceBean{ privateStringname; privatePersonDaopersondao;
publicvoidsetName(Stringname){ =name; }
publicvoidsetPersonDao(PersonDaopersondao){ this.persondao=persondao; } ......}屬性注入示例:屬性注入配置:<beanid=“personDao”class=“com.sise.dao.impl.PersonDaoBean”/><beanid="personService"class="com.sise.service.impl.PersonServiceBean"><propertyname="name"value="AA"/><propertyname="persondao"ref="personDao"/></bean>15.3構造器注入構造器注入,通過構造器的參數注入Bean的屬性值。優點:可以保證一些屬性在Bean實例化時得到設置,保證了Bean在實例化后即可使用。語法:必須提供帶參數的構造函數在配置文件中必須使用<constructor-arg>配置構造器參數的注入值packagecom.sise.spring.lesson2.bean;publicclassPersonBean{ privateStringname; privateintage;
publicPersonBean(Stringname,intage){ =name; this.age=age; } ......}構造器注入示例:構造器注入配置:<beanid="personBean"class="
com.sise.spring.lesson2.bean.PersonBean">
<constructor-argindex=“0”value=“AA”/><constructor-argindex=“1”value=“26”/></bean>構造函數第一個參數的索引為0,第二個為1,以此類推構造器注入參數匹配設置:Spring的配置文件采用和元素標簽順序無關的策略。在配置文件中,<constructor-arg>標簽和基本類型參數的對應關系可通過標簽的type和index屬性來決定JAVA反射機制可以獲取構造函數參數的類型,所以當Bean構造函數中的參數的類型是彼此可辨別時,也可不設置type和index構造器注入配置:<beanid="personBean"class="com.sise.spring.lesson2.bean.PersonBean">
<constructor-argindex=“0”type=“java.lang.String”
value=“AA”/>
<constructor-argindex=“1”type=“int”
value=“26”/></bean>
15.4依賴注入值的配置注入到Bean中的值可以包括以下幾種類型值:字面值其他Bean實例
集合類型對象1.注入字面值:“字面值”通常指基本數據類型及其封裝類以及String等類型的值,這些值可通過<property>或<constructor-arg>的value屬性進行設置。注入配置:<beanid="personService“class="com.sise.service.impl.PersonServiceBean"><propertyname=“age”value=“26”/><propertyname=“name”value=“張三”/></bean>2.注入其他Bean實例:注入其他Bean實例有三種方式:使用<property>或<constructor-arg>標簽中的ref屬性引用使用<ref>子標簽引用使用內部Bean①使用<property>標簽的ref屬性引用注入配置:<beanid="personService“class="com.sise.service.impl.PersonServiceBean"><propertyname=“personDao”ref=“personDao”/></bean><beanid="personDao"class="com.sise.dao.impl.PersonDaoBean“/>②使用<ref>子標簽引用:在<property>中使用子標簽<ref>可以引用IoC容器中定義的Bean。<ref>常使用以下兩個屬性來引用容器中的其他Bean:bean:用于引用同一個XML文件或不同XML文件中Bean的id或name屬性值。注意:當引用不同XML的Bean時需要使用import引入其他配置文件到當前XML文件local:用于引用同一XML文件中的Bean的id或name屬性值使用<ref>子標簽引用示例:注入配置:<beanid="personService“class="com.sise.service.impl.PersonServiceBean"><propertyname="persondao">
<refbean="personDao"/><!--<reflocal="personDao"/>-->
</property></bean><beanid="personDao"class="com.sise.dao.impl.PersonDaoBean"/>③使用內部bean如果一個bean只被某個bean引用,此時可使用內部bean的方式進行bean的注入,例如:注入配置:<beanid="personService"class="com.sise.service.impl.PersonServiceBean"><propertyname=“persondao”>
<bean
class=“com.sise.dao.impl.PersonDaoBean“/></property></bean>不需要設置id屬性,該bean只能被personService所引用,不能被其他bean引用注入字面值及Bean實例示例:①創建一個java項目②搭建Spring運行環境(即對項目添加相應的JAR文件)③創建一個Dao接口及其實現類④創建一個業務接口及其實現類⑤在src目錄下新建一個Spring的配置文件⑥創建測試類④創建Dao接口及其實現類:⑤創建業務接口:⑤創建業務類:⑥創建Spring配置文件:⑦創建測試類:3.注入集合類型對象:Spring為List、Set、Map和Properties等集合類屬性的注入提供了專門的配置元素標簽。注入集合類型對象示例:packagecom.sise.service.impl;publicclassPersonServiceBean{
privateSet
sets=newHashSet(); privateList
lists=newArrayList(); privateMapmaps=newHashMap(); privateProperties
properties=new
Properties(); //省略各屬性的setter方法}1)注入Set類型對象的配置:<beanid="personService“class="com.sise.service.impl.PersonServiceBean">
<propertyname="sets">
<set> <value>set1</value> <value>set2</value> <refbean=“myDataSource”> </set>
</property></bean>使用<set>和<value>或<ref>來配置Set類型對象2)注入List類型對象的配置:<beanid="personService“class="com.sise.service.PersonServiceBean">
<propertyname=“lists"> <list>
<value>list1</value> <value>list2</value> <refbean=“myDataSource”>
</list> </property></bean>使用<list>和<value>或<ref>來配置List類型對象。3)注入Map類型對象的配置:<beanid="personService“class="com.sise.serv.PersonServiceBean">
<propertyname=“maps">
<map> <entrykey=“item1"value=“item1_value"/>
<entrykey=“item2"value=“item2_value"/><entrykey=“item3"value-ref="myDataSource"/></map></property></bean>使用<map>、<entry>來配置Map類型對象。4)注入Properties類型對象的配置<beanid="personService“class="com.sise.service.PersonServiceBean">
<propertyname=“properties"> <props> <propkey=“p1”>item1</prop> <propkey=“p2”>item2</prop></props> </property></bean>Properties類型是Map類型的特例:Map元素的鍵和值可以是任何類型,而Properties屬性的鍵和值只能是字符串。使用<props>和<prop>標簽來配置注意:沒有<value>子標簽15.5注解注入在java代碼中可以使用@Autowired或@Resource注解方式進行依賴注入,此時需要在項目中添加spring-aop-4.2.3.RELEASE.jar以及在XML配置文件中添加以下配置信息:<beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"xsi:schemaLocation=/schema/beans/schema/beans/spring-beans-4.2.xsd
/schema/context/schema/context/spring-context-4.2.xsd”><context:annotation-config/>這個配置隱式注冊了多個對注解進行解析處理的處理器,如:AutowiredAnnotationeanPostProcessor,CommonAnnotationPostProessor等@Resource注解存在JDK1.6中的javax.annotation包下@Autowired注解由Spring框架提供,與框架耦合緊密建議使用@Resource使用JAVAEE5或JDK1.6時添加該包@Autowired:默認按類型裝配@Resource:默認按字段名稱或屬性名稱裝配,當找不到與名稱匹配的bean時才會按類型裝配如果設置為:@Resource(name="xxx")時,將按“xxx“尋找bean,沒有匹配的bean時返回null@Autowired和@Resource既可對字段進行注入,也可對屬性進入注入何謂同類型?//用于字段上@Resource
privatePersonDaopersonDao;//為注解指定名字@Resource(name="personDaoBean")privatePersonDaopersonDao;//用于屬性的setter上@Resource
publicvo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 ISO 14971:2007 RU Medical devices - Application of risk management to medical devices
- 【正版授權】 ISO 11137-1:2025 EN Sterilization of health care products - Radiation - Part 1: Requirements for the development,validation and routine control of a sterilization process f
- 【正版授權】 IEC TS 62271-315:2025 EN High-voltage switchgear and controlgear - Part 315: Direct current (DC) transfer switches
- 【正版授權】 IEC 61479:2001+AMD1:2002 CSV FR-D Live working - Flexible conductor covers (line hoses) of insulating material
- 【正版授權】 IEC 60383-1:1993 FR-D Insulators for overhead lines with a nominal voltage above 1000 V - Part 1: Ceramic or glass insulator units for a.c. systems - Definitions,test metho
- 【正版授權】 IEC 60730-1:1999+AMD1:2003 CSV FR-D Automatic electrical controls for household and similar use - Part 1: General requirements
- 【正版授權】 IEC 60309-1:1999+AMD1:2005 CSV FR-D Plugs,socket-outlets and couplers for industrial purposes - Part 1: General requirements
- 【正版授權】 IEC 60076-11:2004 FR-D Power transformers - Part 11: Dry-type transformers
- 保險行業檔案管理培訓
- 小學趣味排簫課件
- 【9物一?!堪不蘸戏尸幒^2025年中考物理一模試卷
- 【計算機應用基礎試題】上海大學2022年練習題匯總(附答案解析)
- 中考化學復習的策略課件
- 保潔常用工具和設備一覽表
- 橋架出廠檢驗報告
- 《中國古典園林史》課件第四章園林的全盛期-隋唐
- 加拿大介紹-PPT課件
- 漢中市城鎮職工基本醫療保險門診慢性病申請鑒定表
- 招投標項目評分表,招標評分
- 盤扣式腳手架模板與支撐架專項施工方案
- 每天堅持一小時體育鍛煉
評論
0/150
提交評論