国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

JavaWeb Spring依賴注入深入學(xué)習(xí)

2019-11-26 13:51:53
字體:
供稿:網(wǎng)友

一、依賴注入(DI)

依賴注入聽起來很高深的樣子,其實(shí)白話就是:給屬性賦值。一共有兩種方法,第一是以構(gòu)造器參數(shù)的形式,另外一種就是以setting方法的形式。

1 構(gòu)造器注入

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

使用xml的注入方式

A. 通過參數(shù)的順序

<constructor-arg index="0"><value>張三</value></constructor-arg>
<constructor-arg index="1"><value>56</value></constructor-arg>

 B. 通過參數(shù)的類型

<constructor-arg type="java.lang.Integer"><value>56</value></constructor-arg>
<constructor-arg type="java.lang.String"><value>張三</value></constructor-arg>

具體實(shí)例

假如現(xiàn)在要對(duì)一個(gè)Person類注入?yún)?shù),Student是一個(gè)另外一個(gè)類。

public class Person { private String pid; private String name; private Student student; public Person(String pid, Student student){  this.pid= pid;  this.student = student; } public Person(String pid, String name){  this.pid = pid;  this.name = name; }}

配置applicationContext.xml,假如不進(jìn)行參數(shù)配置,則報(bào)錯(cuò),找不到相應(yīng)的構(gòu)造器。配置了相應(yīng)的參數(shù),則應(yīng)在類中聲明相應(yīng)的構(gòu)造函數(shù)。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="person" class="com.itheima10.spring.di.xml.constructor.Person">  <!--    不配參數(shù),將會(huì)采取默認(rèn)的構(gòu)造器   constructor-arg person類中某一個(gè)構(gòu)造器的某一個(gè)參數(shù)    index 為參數(shù)的角標(biāo)    type 參數(shù)的類型    value 如果為基礎(chǔ)屬性,則用這個(gè)賦值    ref 引用類型賦值   -->  <constructor-arg index="0" type="java.lang.String" value="aaa"></constructor-arg>  <constructor-arg index="1" ref="student"></constructor-arg> </bean> <bean id="person1" class="com.itheima10.spring.di.xml.constructor.Person">  <property name="pid" value="1"></property> </bean> <bean id="student" class="com.itheima10.spring.di.xml.constructor.Student"></bean></beans>

編寫測(cè)試類DIXMLConstructorTest ,進(jìn)行斷點(diǎn)調(diào)試,將會(huì)發(fā)現(xiàn)根據(jù)配置的參數(shù),進(jìn)入的構(gòu)造函數(shù)是Person(String pid, Student student)

public class DIXMLConstructorTest { @Test public void test1(){  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  Person person = (Person) context.getBean("person"); } }

2 使用屬性setter方法進(jìn)行注入

使用xml的注入方式:

A. 簡(jiǎn)單Bean的注入
簡(jiǎn)單Bean包括兩種類型:包裝類型和String

<bean id="personService" class="com.itcast.bean.impl.PersonServiceImpl"><!-- 基本類型,string類型 --><property name="age" value="20"></property><property name="name" value="張無忌"></property> </bean>

B. 引用其他Bean

<bean id="person" class="com.itcast.bean.Person" /> <bean id="personService" class="com.itcast.bean.impl.PersonServiceImpl"> <property name="person" ref="person" /></bean>

1.1 裝配list集合

<property name="lists"> <list>  <value>list1</value>  <value>list2</value>  <ref bean="person" /> </list></property>

1.2 裝配set集合

<property name="sets"> <set>  <value>list1</value>  <value>list2</value>  <ref bean="person" /> </set></property>

1.3 裝配map

<property name="maps">    <map>     <entry key="01">       <value>map01</value>     </entry>     <entry key="02">       <value>map02</value>     </entry>     </map></property>

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

1.4 裝配Properties

<property name="props"> <props>  <prop key="01">prop1</prop>  <prop key="02">prop2</prop> </props></property> 

 具體實(shí)例

1.創(chuàng)建兩個(gè)對(duì)象Person和Student

package xgp.spring.demo;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class Person { private String pid; private String name; private Student student; private List lists; private Set sets; private Map map; private Properties properties; private Object[] objects; public Person(){  System.out.println("new person"); } //省略getter和setter方法}
package xgp.spring.demo;public class Student { public Student(){  System.out.println("new student"); } public void say(){  System.out.println("student"); }}

配置applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--   把person和student放入到spring容器中  property 用來描述Person類的屬性  value 如果是一般屬性,則用value賦值  ref 如果該屬性是引用類型,用ref賦值  --> <bean id="person" class="com.itheima10.spring.di.xml.setter.Person"   init-method="init"   lazy-init="true">  <property name="pid" value="1"></property>  <property name="name" value="王二麻子"></property>  <property name="student" ref="student"></property>  <property name="lists">   <list>    <value>list1</value>    <value>list2</value>    <ref bean="student"/>   </list>  </property>  <property name="sets">   <set>    <value>set1</value>    <value>set2</value>    <ref bean="student"/>   </set>  </property>  <property name="map">   <map>    <entry key="entry1">     <value>map1</value>    </entry>    <entry key="entry2">     <ref bean="student"/>    </entry>   </map>  </property>  <property name="properties">   <props>    <!--      不需要引用類型     -->    <prop key="prop1">prop1</prop>    <prop key="prop2">prop2</prop>   </props>  </property>  <property name="objects">   <list>    <value>aa</value>    <value>bb</value>   </list>  </property> </bean> <bean id="student" class="com.itheima10.spring.di.xml.setter.Student"></bean></beans>

編寫測(cè)試類DIXMLSetterTest

package xgp.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import xgp.spring.demo.Person;public class DIXMLSetterTest { /**  * spring 容器做的事情:  *  1、spring容器做了什么?(1)啟動(dòng)spring容器  *       (2)為person和student兩個(gè)bean創(chuàng)建對(duì)象  *       (3)解析property的name屬性,拼接setter方法,解析property的  *        value或者ref屬性,給setter方法傳遞參數(shù),利用反射技術(shù)給對(duì)象賦值。  *       (4)從spring容器中,把對(duì)象提取出來,對(duì)象調(diào)用方法。  *  2、spring容器執(zhí)行順序是什么?   */ @Test public void test1(){  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  Person person = (Person) context.getBean("person");  System.out.println(person.getPid());  System.out.println(person.getName());  System.out.println(person.getLists());  System.out.println(person.getSets());  System.out.println(person.getMap());  System.out.println(person.getObjects().length); }}/*1王五[list1, list2, xgp.spring.demo.Student@76a9b9c][set1, set2, xgp.spring.demo.Student@76a9b9c]{entry1=map1, entry2=map2}2*/

spring容器的執(zhí)行順序

1.都是默認(rèn)設(shè)置

2.設(shè)置student(lazy-init=true)

3.設(shè)置person(lazy-init=true)

總結(jié)
可以采用兩種方法注入?yún)?shù),構(gòu)造器要寫對(duì)應(yīng)的構(gòu)造函數(shù),setter要生成相應(yīng)的setter方法,并編寫默認(rèn)的構(gòu)造器。

2.5 IOC與DI的意義

學(xué)了這些,發(fā)現(xiàn)有什么意義?下面寫個(gè)文檔管理系統(tǒng)例子來說明,需求見下圖

1.編寫Document 接口

public interface Document { public void read(); public void write();}

2、編寫實(shí)現(xiàn)類WordDocument ,ExcelDocument ,PDFDocument

public class WordDocument implements Document{ public void read() {  System.out.println("word read"); } public void write() {  System.out.println("word write"); }}

3、編寫文檔管理 系統(tǒng) DocumentManager

public class DocumentManager { private Document document; public void setDocument(Document document) {  this.document = document; } public DocumentManager(){  } public DocumentManager(Document document) {  super();  this.document = document; } public void read(){  this.document.read(); } public void write(){  this.document.write(); }}

4、編寫測(cè)試類DocumentTest

/** * 利用ioc和di能做到完全的面向接口編程 * */public class DocumentTest { /**  * Document document = new WordDocument();  * 這行代碼是不完全的面向接口編程,因?yàn)榈忍?hào)的右邊出現(xiàn)了具體的類  */ @Test public void testDocument_NOSPRING(){  Document document = new WordDocument();  DocumentManager documentManager = new DocumentManager(document);  documentManager.read();  documentManager.write(); } /**  * 在代碼端不知道Document是由誰(shuí)來實(shí)現(xiàn)的,這個(gè)是由spring的配置文件決定的  * <bean id="documentManager"    class="com.itheima10.spring.iocdi.document.DocumentManager">   <!--     document為一個(gè)接口    -->   <property name="document">    <!--      wordDocument是一個(gè)實(shí)現(xiàn)類,賦值給了document接口     -->    <ref bean="pdfDocument"/>   </property>  </bean>  */ @Test public void testDocument_Spring(){  ApplicationContext context =      new ClassPathXmlApplicationContext("applicationContext.xml");  DocumentManager documentManager =(DocumentManager)context.getBean("documentManager");  documentManager.read();  documentManager.write(); }}

從上面可以看出不適用spring和適用spring的區(qū)別

<!--   documentManager,wordDocument,excelDocument,pdfDocument放入到spring容器中  --> <bean id="wordDocument" class="com.itheima10.spring.iocdi.document.WordDocument"></bean> <bean id="excelDocument" class="com.itheima10.spring.iocdi.document.ExcelDocument"></bean> <bean id="pdfDocument" class="com.itheima10.spring.iocdi.document.PDFDocument"></bean> <bean id="documentManager"   class="com.itheima10.spring.iocdi.document.DocumentManager">  <!--    document為一個(gè)接口   -->  <property name="document">   <!--     wordDocument是一個(gè)實(shí)現(xiàn)類,賦值給了document接口    -->   <ref bean="pdfDocument"/>  </property> </bean>

使用spring只需要在applicationContext中配置相應(yīng)的<ref bean="">對(duì)象,而不需要關(guān)注具體的實(shí)現(xiàn)類,實(shí)現(xiàn)完全的面向接口編程,這也是為什么spring能夠和這么多工具集成的原因。

2.6 mvc實(shí)例

主站蜘蛛池模板: 三亚市| 长泰县| 寻甸| 湖南省| 裕民县| 兴山县| 汤阴县| 黎川县| 汪清县| 临泽县| 姚安县| 黔东| 尉犁县| 温宿县| 云阳县| 什邡市| 尉氏县| 金门县| 大名县| 汝南县| 红桥区| 博白县| 蛟河市| 毕节市| 武清区| 洮南市| 抚松县| 台北市| 宁夏| 花垣县| 遂平县| 连城县| 光山县| 民权县| 龙岩市| 商水县| 桂林市| 哈巴河县| 星子县| 杭州市| 霍邱县|