日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)

發(fā)布時(shí)間:2023/12/13 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring_01 spring容器、控制反转(IOC)、依赖注入(DI) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  1 什么是spring框架

  2 spring框架的特點(diǎn)

  3 spring容器

    3.1 什么是spring容器

    3.2 spring容器創(chuàng)建對(duì)象的編程步驟

    3.4 spring容器創(chuàng)建對(duì)象的方式

    3.5 bean元素的幾個(gè)重要屬性

  4 IOC

    4.1 什么是IOC

    4.2 什么事DI

    4.3 DI的三種方式

1 什么是spring框架

  是一個(gè)開(kāi)源的用來(lái)簡(jiǎn)化企業(yè)級(jí)應(yīng)用開(kāi)發(fā)的框架

?

2 spring框架的特點(diǎn)

  2.1?簡(jiǎn)化開(kāi)發(fā)

    spring對(duì)一些常見(jiàn)的api(比如jdbc)做了封裝,使用這些封裝之后的api,代碼會(huì)大大簡(jiǎn)化。
  比如,使用springjdbc來(lái)訪問(wèn)數(shù)據(jù)庫(kù),就不用考慮如何獲取連接,關(guān)閉連接等操作。

  2.2 管理對(duì)象

    spring可以幫我們管理對(duì)象之間的依賴關(guān)系,這樣一來(lái), 軟件更容易維護(hù)。

  2.3 集成其它框架

    spring可以將一些框架集成進(jìn)來(lái),更方便使用這些框架。、
  比如,可以利用spring集成mybatis(mybatis是一個(gè)用 來(lái)訪問(wèn)數(shù)據(jù)庫(kù)的框架),這樣mybatis用起來(lái)更加簡(jiǎn)單。?

?

3 spring容器

  3.1 什么是spring容器

    是spring框架當(dāng)中的一個(gè)核心模塊,用來(lái)管理對(duì)象。

  3.2怎么利用 spring容器 來(lái)創(chuàng)建對(duì)象

    3.2.1 創(chuàng)建一個(gè) maven項(xiàng)目

      》記得讓maven項(xiàng)目中出現(xiàn) web.xml 這個(gè)配置文件 -->> 還記得咋整嗎?

    3.2.2 導(dǎo)包

      spring-webmvc -->> 啟動(dòng) spring容器 時(shí)需要用到
      junit -->> 進(jìn)行單元測(cè)試時(shí)需要用到

    3.2.3 啟動(dòng) spring容器

      》添加一個(gè) spring容器 配置文件

        例:給Student類配置bean,只需在spring的配置文件中添加

          <bean id="stu" class="test.Student"></bean>

            id : 必須要保證唯一

            class:就是需要配置bean的類名,但是必須在前面加上 包名.
      》利用 ApplicationContext 的實(shí)現(xiàn)類 ClassPathXmlApplicationContext 去啟動(dòng)容器

    3.2.4 利用 getBean(String name, Class<T> requiredType) 來(lái)實(shí)例化對(duì)象

      注意:spring容器會(huì)利用相關(guān)類的無(wú)參構(gòu)造器去創(chuàng)建實(shí)例,所以相關(guān)類中必須要有無(wú)參構(gòu)造器,否則會(huì)報(bào)錯(cuò):“找不到無(wú)參構(gòu)造器”

       

  3.3 注意

    spring容器一旦啟動(dòng),就會(huì)在 堆 中將所有配置了 bean 的類創(chuàng)建好一個(gè)實(shí)例

1 package test; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable { 6 private Integer id; 7 private String name; 8 private String gender; 9 10 11 public Student() { 12 super(); 13 System.out.println("New Student()"); 14 } 15 public Integer getId() { 16 return id; 17 } 18 public void setId(Integer id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 public String getGender() { 28 return gender; 29 } 30 public void setGender(String gender) { 31 this.gender = gender; 32 } 33 34 public String toString() { 35 return "Student [id=" + id + ", name=" + name + ", gender=" + gender + "]"; 36 } 37 38 } Student類 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:jee="http://www.springframework.org/schema/jee" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 12 xsi:schemaLocation=" 13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 22 23 <bean id="stu" class="test.Student"></bean> 24 25 </beans> test.xml配置文件 1 package test; 2 3 import java.io.Serializable; 4 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class Test implements Serializable { 9 public static void main(String[] args) { 10 ApplicationContext ac = new ClassPathXmlApplicationContext("test.xml"); 11 System.out.println(ac); 12 13 Student stu1 = ac.getBean("stu", Student.class); 14 System.out.println(stu1); 15 } 16 } Test類

?

  3.4spring容器 實(shí)例化對(duì)象的三種方法

    3.4.1?利用無(wú)參構(gòu)造器實(shí)現(xiàn)(很重要)?

      必須有無(wú)參構(gòu)造器,如果已經(jīng)添加了有參構(gòu)造器,那么默認(rèn)的無(wú)參構(gòu)造器就會(huì)被覆蓋;此時(shí)就需要手動(dòng)添加一個(gè)無(wú)參構(gòu)造器

    3.4.2?利用靜態(tài)工廠方法實(shí)現(xiàn)(了解)

    3.4.3?利用實(shí)例化工廠方法實(shí)現(xiàn)(了解)

?  3.5 bean元素的幾個(gè)重要屬性

    3.5.1 作用域

      scope 屬性 : 用來(lái)指定作用域

      缺省值是 singleton(單例),如果值是prototype(原型),則可以創(chuàng)建多個(gè)對(duì)象,否則不可以

      我們一般使用 單例模式 就可以啦(即:默認(rèn)值)

    3.5.2?生命周期

      初始化

        init-method屬性:指定初始化方法

      銷毀

        destroy-method屬性:指定銷毀方法

        只有作用域?yàn)閱卫?bean, 銷毀方法才有效

        必須使用這個(gè)spring容器接口:AbstractApplicationContext

    3.5.3 延遲加載

      默認(rèn)情況下當(dāng) spring容器 啟動(dòng)之后,會(huì)將所有作用域?yàn)閱卫?bean 都創(chuàng)建好

      lazy-init屬性:指定是否延遲加載,值為 true 時(shí)延遲加載(一般不用延遲加載)

?

4 IOC(Inversion Of Controll 控制反轉(zhuǎn))

  4.1 什么事IOC

    對(duì)象之間的依賴關(guān)系交給容器來(lái)管理。

  4.2?什么是DI (Dependency Injection 依賴注入)

    容器通過(guò)調(diào)用set方法或者構(gòu)造器來(lái)建立對(duì)象之間的依賴關(guān)系

    注:IOC是目標(biāo),而DI是手段

?

  4.3DI 注入的兩種方式

    4.3.1?利用 set 方法完成依賴注入(掌握)

      注入類中必須有一個(gè)成員變量,該成員變量的類型必須是被依賴注入的類

      注入類中必須實(shí)現(xiàn)需要注入的那個(gè)成員變量的 set 方法

      set 方法進(jìn)行依賴注入時(shí),在配置文件中用到的是 property標(biāo)簽;如果需要注入的類中有有參構(gòu)造器,那么必須實(shí)現(xiàn)無(wú)參構(gòu)造器,因?yàn)橛袇?gòu)造器會(huì)覆蓋無(wú)參構(gòu)造器(注意:菜鳥(niǎo)一般吧無(wú)參構(gòu)造器和有參構(gòu)造器都實(shí)現(xiàn))

      注意:注入類中那個(gè)成員變量的類型 一般都設(shè)定為被注入類的一個(gè)接口,這樣有利于今后進(jìn)行維護(hù)

      利用 property標(biāo)簽實(shí)現(xiàn)依賴注入

        <property name="stu" ref="student01"></property>

          name:需要依賴注入的成員變量

          ref:需要依賴注入類的id屬性值

?

      

      圖解:創(chuàng)建A的實(shí)例,而且B是注入到A中的;B類中必須實(shí)現(xiàn)無(wú)參構(gòu)造器,A中必須添加一個(gè)類型為B的成員變量,而且還必須為該成員變量實(shí)現(xiàn)set方法,在spring配置文件中配置A和B的bean,而且在A的bean中還要利用property標(biāo)簽來(lái)實(shí)現(xiàn)B的依賴注入

    4.3.2?利用有參構(gòu)造器完成依賴注入(掌握)

      這里的構(gòu)造器是有參構(gòu)造器,但是它的參數(shù)這是你需要注入的參數(shù)名,不包含其他的

?

      利用constructor-arg標(biāo)簽實(shí)現(xiàn)依賴注入

        <constructor-arg index="0" ref="b1"/>

          index:有參構(gòu)造器中參數(shù)的位置,從0開(kāi)始

          ref:依賴注入類的id屬性值

      

      圖解:創(chuàng)建A的實(shí)例,而且B是注入到A中的;B類中必須實(shí)現(xiàn)無(wú)參構(gòu)造器,A中必須添加一個(gè)類型為B的成員變量;為A添加一個(gè)有參構(gòu)造器,而且該有參構(gòu)造器的參數(shù)只是你要注入的那個(gè)成員變量,不包含其他的;而且該有參構(gòu)造器任然會(huì)覆蓋無(wú)參構(gòu)造器,所以如果想要使用無(wú)參構(gòu)造器就必須先實(shí)現(xiàn)無(wú)參構(gòu)造器;在spring的配置文件中配置A和B的bean,而且還需要用constructor-arg標(biāo)簽實(shí)現(xiàn)B的依賴注入

?

    4.3.3?自動(dòng)裝配(一般不用)

    注意:依賴注入一般都由注釋實(shí)現(xiàn)

?

源代碼鏈接:點(diǎn)擊前往

轉(zhuǎn)載于:https://www.cnblogs.com/NeverCtrl-C/p/6849543.html

總結(jié)

以上是生活随笔為你收集整理的Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。