一、IOC概念
IOC-Inversion of Control,控制反轉(zhuǎn),是一種設(shè)計思想,在一般的java中,我們通過new來構(gòu)建對象,而在Spring中,我們把對象的構(gòu)建交給Spring容器,這樣做的好處是可以降低代碼之間的耦合,不用關(guān)系對象之間的依賴關(guān)系
二、IOC容器對象
BeanFactory
BeanFactory是一個基類,提供了最基本的功能,采用延遲加載,即使用到類的時候才加載,適用于資源有限且功能不是很嚴(yán)格的場景
ApplicationContext
是BeanFactory 的子類,他在啟動時就完成所有初始化,還提供了特有的方法
三、DI
DI-dependency Injection,依賴注入,是IOC的另外一種描述方式,指的是將一個對象注入到另外一個對象之中,這里體現(xiàn)的是Spring的對象依賴管理
四、xml管理bean
-
構(gòu)造方法注入屬性
- 在xml配置中,如果需要創(chuàng)建類的實例,必須使用
bean節(jié)點,節(jié)點有id和name屬性,用于獲取這個實例,如果都不指定,則以類名首字母小寫獲取 - 如果想要初始化構(gòu)造函數(shù),可以添加
constructor-arg節(jié)點,index代表參數(shù)的位置,也可以用type指定
<?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.xsd">
<!--ID,name:當(dāng)有ID或者name或者都有時,可以分別根據(jù)名字創(chuàng)建-->
<bean id="hello" class="com.spring.imp.HelloImp">
<!--給構(gòu)造函數(shù)傳入?yún)?shù),index=0代表第1個參數(shù),其中除了可以用索引位置表示以外,還可以用類型和參數(shù)名稱-->
<constructor-arg index="0" value="Hello Spring" />
<constructor-arg type="int" value="2" />
</bean>
-
通過set輸入屬性
可以通過bean的set方法注入屬性
<bean name="setByMethod" class="com.spring.imp.HelloImp">
<!--需要注意的是:必須要有無參構(gòu)造函數(shù)-->
<property name="message" value="hello world"></property>
<property name="inde">
<value>1</value>
</property>
</bean>
-
注入對象
如果對象中有引用屬性,則可以通過ref指向Spring容器中另外一個對象的名字即可
<!--注入對象 通過構(gòu)造器注入-->
<bean id="decorator1" class="com.spring.imp.HelloApiDecorator">
<constructor-arg index="0" ref="hello" />
</bean>
-
注入集合、map
```xml
<!--注入List-->
<!--
1.可選value-type屬性,表示存入list的類型,比如value-type=java.lang.Sring
2.也可以使用范型,Spring能根據(jù)范型數(shù)據(jù)自動檢測List里的數(shù)據(jù)類型
3.如果沒有指定value-type,List也不是范型,則默認(rèn)為String類型
-->
<bean id="listBean" class="com.spring.imp.ListBean">
<property name="values">
<!--list 可選參數(shù)value-type-->
<list>
<value>1</value>
<value>asd</value>
<value>3</value>
</list>
</property>
<!--注入set,和list類似-->
<property name="integers">
<set>
<value>11</value>
<value>112</value>
<value>1123</value>
</set>
</property>
<!--注入一維數(shù)組-->
<property name="arr1">
<array value-type="java.lang.String">
<value>注入一維</value>
<value>數(shù)組</value>
</array>
</property>
<!--注入二維數(shù)組-->
<property name="arr2">
<array>
<array>
<value>aa</value>
<value>aa</value>
</array>
<array>
<value>bb</value>
<value>bb</value>
</array>
</array>
</property>
<!--注入Map-->
<property name="map">
<!--設(shè)置key和value的類型-->
<map key-type="java.lang.String" value-type="java.lang.String">
<entry>
<!--設(shè)置一個鍵值對,方法1-->
<key><value>name1</value></key>
<value>zhangsan</value>
</entry>
<!--方法2-->
<entry key="name2" value="里斯"/>
</map>
</property>
<!--注入-->
</bean>
<!--注入null可以使用set注入 <null/>標(biāo)簽-->
五、通過注解裝配對象
以上是完全通過xml配置管理對象,也可以通過注解掃描的方式管理,在class定義上,可以添加以下注解,實際上Component代表要創(chuàng)建類的實例,其他注解只是為了更加語義化
Repository:對DAO層,表示這個類要被初始化
Service:對Service,同上
Controller:對Controller,同上
Component:對所有
然后對有以上注解的類實例化,他會掃描com.test包下所有被注解的類,然后實例化
<context:annotation-config />
<context:component-scan base-package="com.test" />
六、自動裝配
通過上面的注解把對象交給Spring容器管理以后,就需要給這些對象賦值,可以通過@Autowire,他會自動找到匹配的,前提是你沒有在Component等注解上自定義名字,也就是@Component(name),如果指定了名字,則Autowire也需要指定名字,Autowire是Spring特有的注解,也可以使用Inject,他們大多情況下是等效的
七、 Bean的作用域
被Spring創(chuàng)建的Bean有各自的作用域,具體如下
Singleton:在Spring容器中指存在一個實例,在Spring中獲取的都是同一個對象
<bean class="com.spring.test" scope="singleton" />
prototype:原型,每次獲取bean都會新建一個bean
request:針對web項目,每次請求返回一個bean
session:針對web項目,每次會話返回一個bean