在spring框架中有兩個(gè)重要的特征即:控制反轉(zhuǎn)(IOC)和面向切面(AOP)
什么是IOC
IOC:即控制反轉(zhuǎn)吏颖,控制權(quán)的轉(zhuǎn)移搔体,應(yīng)用程序本身不負(fù)責(zé)依賴(lài)對(duì)象的創(chuàng)建和維護(hù),而是由外部容器負(fù)責(zé)創(chuàng)建和維護(hù)
DI(依賴(lài)注入)是其一種實(shí)現(xiàn)方式 目的:創(chuàng)建對(duì)象并且組裝對(duì)象之間的關(guān)系 ioc容器會(huì)幫助我們自動(dòng)創(chuàng)建對(duì)象
IOC作用:找IOC容器-容器返回對(duì)象-使用容器
DI-IOC的另一表達(dá)半醉,即組件以一些預(yù)先定義好的方式接受來(lái)自如容器的資源注入
在spring容器中默認(rèn)僅存在一個(gè)Bean實(shí)例疚俱,Bean以單實(shí)例的方式存在,即無(wú)論實(shí)例化多少個(gè)bean對(duì)象它們的引用都是相等的
什么是DI
DI即依賴(lài)于某種方法給Bean的屬性賦值缩多,DI有三種賦值方式
- 屬性注入 構(gòu)造器set方法注入 通過(guò)xml文件中<property name="屬性名" value="屬性值"></property>或p屬性注入
- 構(gòu)造器注入 <constructor-arg value="屬性值" index="構(gòu)造器中下標(biāo)位置(可省)"></constructor-arg>//構(gòu)造器注入資源時(shí)按順序進(jìn)行
- 工廠注入
配置Spring配置文件
- 通過(guò)<property>標(biāo)簽注入
<bean id="person1" class="summer.spring.bean.Person">
<property name="name" value="zhangsan"></property>
<property name="age" value="12"></property>
</bean>
name屬性的值對(duì)應(yīng)javaBean中定義的屬性呆奕,value即為要給屬性賦的值
- <constructor-arg index>順序注入
<bean id="car1" class="summer.spring.bean.Car" >
<constructor-arg value="奔馳" index="0"></constructor-arg>
<constructor-arg value="德國(guó)" index="1"></constructor-arg>
<constructor-arg value="3000000.0" index="2"></constructor-arg>
</bean>
index索引對(duì)應(yīng)javaBean中第幾個(gè)屬性,從0開(kāi)始
- 從外部屬性文件中注入
配置properties文件衬吆,與Bean的屬性相對(duì)應(yīng)
personName=\u674E\u56DB\u5F20\u4E09
personAge=55
配置Spring文件
<context:property-placeholder location="classpath:Person.properties"/>通過(guò)context:property-placeholder標(biāo)簽引入
<bean id="person1" class="spring2.bean.Person" >
<property name="name" value="${personName}"></property>
<property name="age" value="${personAge}"></property>
</bean>
${}是SPEL表達(dá)式梁钾,其中的值與properties文件定義的屬性值匹配。
bean的引用
當(dāng)一個(gè)對(duì)象內(nèi)中包含另一個(gè)對(duì)象時(shí)在配置文件中可以通過(guò)<ref>元素或ref屬性為 Bean 的屬性或構(gòu)造器參數(shù)指定對(duì)Bean的引用(id)逊抡,也可以在屬性或構(gòu)造器里包含Bean的聲明姆泻,這樣的Bean稱(chēng)為內(nèi)部Bean
<bean id="car1" class="summer.spring.bean.Car">
<constructor-arg value="奔馳" index="0"></constructor-arg>
<constructor-arg value="德國(guó)" index="1"></constructor-arg>
<constructor-arg value="3000000.0" index="2"></constructor-arg>
</bean>
<bean id="person1" class="summer.spring.bean.Person">
<property name="name" value="zhangsan"></property>
<property name="age" value="12"></property>
<property name="car" ref="car1"></property>
</bean>
id值在整個(gè)spring配置文件中是唯一的!C暗铡拇勃!
引用集合
一個(gè)對(duì)象中包含另一個(gè)對(duì)象的集合時(shí)可以通過(guò)List、Map標(biāo)簽配置
<bean id="person1" class="summer.spring.bean.Person">
<property name="name" value="zhangsan"></property>
<property name="age" value="12"></property>
<property name="cars" >
<list>
<bean class="summer.spring.bean.Car">
<constructor-arg value="奔馳" index="0"></constructor-arg>
<constructor-arg value="德國(guó)" index="1"></constructor-arg>
<constructor-arg value="3000000.0" index="2"></constructor-arg>
</bean>
<bean class="summer.spring.bean.Car">
<constructor-arg value="BMW" index="0"></constructor-arg>
<constructor-arg value="德國(guó)" index="1"></constructor-arg>
<constructor-arg value="2990000.0" index="2"></constructor-arg>
</bean>
</list>
</property>
</bean>
Spring的繼承
<bean id="car1" class="spring2.bean.Car" >
<property name="type" value="BMW"></property>
</bean>
<bean id="person1" class="spring2.bean.Person" autowire="byType">
<property name="name" value="小明"></property>
<property name="car" ref="car1"></property>
</bean>
<bean id="person2" parent="person1"></bean>
person2繼承了person1的所有屬性孝凌,當(dāng)自身屬性于父類(lèi)不一樣時(shí)可以重新在<property>標(biāo)簽中定義自己的屬性值方咆,將會(huì)覆蓋父類(lèi)原有的屬性值。注意abstract和autowire的屬性不會(huì)被子Bean繼承
抽象Bean
<bean id="abstractPerson" abstract="true" >
<property name="name" value="tom"></property>
<property name="car" ref="car1"></property>
</bean>
抽象Bean并不是一個(gè)只是存在的Bean蟀架,而是一個(gè)具有相同一類(lèi)屬性的Bean的一個(gè)模板瓣赂,class屬性可以去掉榆骚,于java中抽象類(lèi)非常類(lèi)似