[TOC]
DIP
一種軟件設(shè)計原則
IOC
一種設(shè)計模式睬隶,DIP的具體實現(xiàn)锣夹。即控制反轉(zhuǎn),將依賴(低層模塊)對象的獲得交給第三方(系統(tǒng))來控制苏潜。
DI
IOC的一種重要的實現(xiàn)方式
IOC容器
spring簡介
作用:IOC容器银萍,控制反轉(zhuǎn),將創(chuàng)建對象的權(quán)利交給容器去做
好處:不用new對象恤左,降低了類與類之間的耦合度
功能:IOC+AOP+DATA+WEB
spring的原理
將bean的類名以及類與類的關(guān)系配置在xml文件中贴唇,通過反射的方式創(chuàng)建對象,并且組裝對象飞袋。
spring快速入門
1戳气、導(dǎo)包
core、context巧鸭、expression、bean
2蹄皱、引入schema文檔(類似dtd文檔)約束xml的文檔
<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">
</beans>
3览闰、通過xml配置bean
<!-- 1、配置bean -->
<bean class="com.hemi.bean.CandidateA" id="canA" />
<bean class="com.hemi.bean.CandidateB" id="canB" />
<bean class="com.hemi.bean.Personnel" id="personnel">
<!-- 通過構(gòu)造函數(shù)將候選人canA注入到人事部中 -->
<constructor-arg name="programme" ref="canB" />
</bean>
4婉宰、創(chuàng)建測試類
//1蟹腾、獲取xml文件娃殖,并且創(chuàng)建出ApplicationContext
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
//2、獲取人事部
Personnel personnel=(Personnel)context.getBean("personnel");
personnel.interview();
注入方式
- 構(gòu)造函數(shù)方式注入
- constructor-arg 構(gòu)造函數(shù)參數(shù)
- type 使用構(gòu)造函數(shù)參數(shù)類型
- name 使用構(gòu)造函數(shù)參數(shù)名
- index 使用位置 0代表構(gòu)造函數(shù)的第一個位置掸屡,1代表第二個位置,依次類推
- constructor-arg 構(gòu)造函數(shù)參數(shù)
<bean class="com.hemi.bean.Personnel" id="personnel">
<constructor-arg index="0" ref="canB" />
<constructor-arg index="1" ref="canA" />
</bean>
- get、set方式注入
- property 代表屬性名稱
- value 屬性值
- ref 對象的引用
- property 代表屬性名稱
<bean class="com.hemi.bean.Personnel" id="personnel">
<property name="name" value="lili"></property>
<property name="programme" ref="canA"></property>
</bean>
- p名稱空間
在文檔定義中添加xmlns:p="http://www.springframework.org/schema/p"
<bean class="com.hemi.bean.Personnel" id="personnel" p:name="lisi"></bean>
總結(jié):
spring ioc容器特點:
1、在啟動的時候會將所有的對象按順序創(chuàng)建完畢
2玫鸟、按需注入
3枚碗、按需獲取
bean參數(shù)詳解
id:對象的名字
destory-method:ioc容器摧毀時創(chuàng)建
init-method:創(chuàng)建對象時執(zhí)行的方法
depends-on:創(chuàng)建對象之前應(yīng)該創(chuàng)建好的對象
lazy-init:延遲創(chuàng)建對象
scope:設(shè)置作用域锡足,singleton(單例)、prototype(多例)壳坪、request舶得、sesssion、global session factory-method:工廠方法
factory-bean:工廠對象
abstract:標(biāo)記為抽象類
注解創(chuàng)建對象
創(chuàng)建對象 @Component @Service @Repository @Controller
//創(chuàng)建對象的時候可以使用參數(shù)爽蝴,設(shè)置對象的引用變量
//如果沒有寫沐批,那么默認(rèn)使用小駝峰命名
@Component("blackBox")
public class BlackBox{}
注意:四者用法一致,一般使用@Service
注解注入對象
注入對象的注解 - @Resource - @Autowired
//name:按照名稱來查找
@Resource(name="blackBox")
private IBox box;
//type:按照類型來查找
@Resource(type=A4Paper.class)
private IPaper paper;
//如果沒有寫蝎亚,那么name就是參數(shù)的變量名 box,所以找不到九孩,然后按照type來查找,IBox類型发框,所以可以找得到
//如果沒有寫躺彬,而內(nèi)存中有多個相同類型的對象,那么就報錯
@Resource
private IBox box1;
//@Autowired不能寫任何參數(shù)
//按照類型來查找梅惯,如果內(nèi)存中有多個相同類型的對象宪拥,那么報錯
//解決問題:使用@Qualifier來指定注入哪個名稱的對象
@Autowired
@Qualifier("blackBox")
private IBox box;
@Autowired
private IPaper paper;
注意:用哪個注解根據(jù)實際需求選擇