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)系的維護就叫做 依賴注入
依賴注入的數(shù)據(jù)類型
- 基本類型和String
- 其他的bean類型(在配置文件中或者注解配置過的bean)
- 復(fù)雜類型/集合類型
注入的方式
- 使用構(gòu)造函數(shù)提供
- 使用set方法提供
- 使用注解提供
構(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>