Spring學(xué)習(xí)筆記day01

Spring筆記

spring IOC 環(huán)境搭建

  • 導(dǎo)入spring依賴

<packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies>

  • resources下新建bean.xml配置文件導(dǎo)入約束

<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.xsd">

  • xml中創(chuàng)建bean對象
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" />

  • 獲取Spring核心容器類
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

  • 根據(jù)配置中的id,從核心容器中獲取想要的類對象
    IAccountService accountService = (IAccountService) ac.getBean("accountService23");

Spring創(chuàng)建bean的3種方式

1、使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建。只有id與class標(biāo)簽沒有其他屬性標(biāo)簽的時候可以用,如果對象中沒有默認(rèn)構(gòu)造函數(shù)則會報錯。

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" />

2、 使用普通工廠中的方法創(chuàng)建對象(使用某個類中的方法創(chuàng)建對象存入容器)

<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"/>

<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"/>

3、 使用靜態(tài)工廠中的靜態(tài)方法創(chuàng)建對象(使用某個類中的靜態(tài)方法創(chuàng)建對象)

 <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"/>

bean的作用范圍

bean標(biāo)簽的scope標(biāo)簽用于指定標(biāo)簽的作用范圍 常用的singleton prototype request 還有個全局的globe啥的...

spring中的依賴注入 dependency injection

什么是依賴注入:
在當(dāng)前類中需要用到其他類對象 由spring為我們提供, 我們只需要在配置文件中說明
依賴關(guān)系的維護(hù)就叫做 依賴注入

依賴注入的數(shù)據(jù)類型

  1. 基本類型和String
  2. 其他的bean類型(在配置文件中或者注解配置過的bean)
  3. 復(fù)雜類型/集合類型

注入的方式

  1. 使用構(gòu)造函數(shù)提供
  2. 使用set方法提供
  3. 使用注解提供

構(gòu)造函數(shù)注入 (只有必須必須使用的時候才用 一般不用)

受用標(biāo)簽: constructor-arg

在bean標(biāo)簽內(nèi)部
type: 要注入的數(shù)據(jù)類型 , 該數(shù)據(jù)類型也是構(gòu)造函數(shù)中某個或某些的數(shù)據(jù)類型
index: 用于指定要注入的數(shù)據(jù)給構(gòu)造函數(shù)中指定索引位置的參數(shù)賦值。 參數(shù)索引的位置從0開始
name : 用于指定給構(gòu)造函數(shù)中指定名稱的參數(shù)賦值 ( 常用的 )
==以上三個用于指定給哪個參數(shù)給構(gòu)造函數(shù)中哪個參數(shù)賦值==
value: 用于基本類型和String類型
ref:用于其他的bean類型數(shù)據(jù)。 它指的就是在spring ioc核心容器中出現(xiàn)過的bean對象

構(gòu)造函數(shù)注入的優(yōu)勢:

在獲取bean對象時,注入數(shù)據(jù)是必須的操作,否則對象無法創(chuàng)建成功

弊端:

改變了bean對象的實例化方式,使我們在創(chuàng)建對象時,如果用不到這些數(shù)據(jù)也必須提供

示例代碼:

 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="Test"/>
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="birthday" ref="now"/>
    </bean>
    <!--    由于構(gòu)造函數(shù)中的DATE對象不能直接用value來賦值 , 所以需要我們再額外配置一個date對象-->
<!--    這句代表含義: spring讀取class標(biāo)簽下的全限定類名, 反射創(chuàng)建一個對象,并且存入spring的核心容器中, 我們可以通過id拿到這個對象 -->
    <bean id="now" class="java.util.Date"/>

set方式注入 (常用)

涉及的標(biāo)簽:property
出現(xiàn)位置:bean標(biāo)簽的內(nèi)部
標(biāo)簽的屬性
name : 用于指定注入時調(diào)用的set方法名稱
value: 用于基本類型和String類型
ref:用于其他的bean類型數(shù)據(jù)。 它指的就是在spring ioc核心容器中出現(xiàn)過的bean對象

優(yōu)勢: 創(chuàng)建對象時沒有明確的限制,可以直接使用默認(rèn)構(gòu)造函數(shù)。
弊端: 如果有某個成員必須有值 , 則獲取對象時有可能set方法沒有執(zhí)行。

復(fù)雜類型的注入 (集合的注入)

用于給List結(jié)構(gòu)集合注入的標(biāo)簽:
list array set
用于給Map 結(jié)構(gòu)集合注入的標(biāo)簽:
map props

示例代碼:
 <bean id="accountService23" class="com.itheima.service.impl.AccountServiceImpl23">
    <property name="myStrs">
        <array>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </array>
    </property>

    <property name="lists">
        <list>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </list>
    </property>

    <property name="mySet">
        <set>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </set>
    </property>

    <property name="myMaps">
        <map>
            <entry key="testA" value="aaa"/>
            <entry key="testB">
                <value>AAA</value>
            </entry>
        </map>
    </property>
    
    <property name="myProps">
        <props>
            <prop key="testC">CCC</prop>
            <prop key="testD">DDD</prop>
        </props>
    </property>

</bean>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容