前置技能:
Java、JDBC、
什麼是IOC:
控制反轉(Inversion of Control),是面向對象程式設計中的一種設計原則,可以降低代碼之間的耦合度。 最常見的實作方式是依賴注入(Dependency Injection)
什麼是DI:
依賴注入(Dependency Injection),A對象依賴B接口類型對象,容器把B接口的一個實作類執行個體化對象指派給A中的屬性b(使A持有一個B借口的實作類對象)。
舉個栗子每個Service對象會持有一個Dao對象,在以往,通常我們需要通過構造器new一個Dao對象。
public class ServiceImpl implements Service {
//聲明本類對象持有一個Dao對象
private Dao myDaoImpl;
//在本類構造器中new一個DaoImple
public ServiceImpl() {
myDaoImpl=new DaoImple();
}
}
但在學習IOC之後,我們隻需聲明Dao,不用為這個屬性指派。
public class ServiceImpl implements Service {
//聲明本類對象持有一個Dao對象
private Dao myDaoImpl;
//Spring的IOC容器會自動為myDaoImpl這個屬性指派
}
這樣做的好處是,當我們要更改Dao時,我們隻需要修改配置檔案,而無需更改已經上線的代碼。
至于為什麼不在原本的代碼上做修改?開閉原則
Getting Started:
1、引入jar包
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLzETN0ETOxEjM5EjNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、建立demo需要的Service和Dao類
Service類及其實作類
public interface Service {
void doSomething();
}
依賴注入的對象需要實作set方法
public class ServiceImpl implements Service {
private Dao daoImpl;
@Override
public void doSomething() {
System.out.println("servie的doSomething方法");
//沒有給daoImpl指派,直接調用其方法
daoImpl.doSomething();
}
//依賴注入的對象需要實作set方法
public void setDaoImpl(Dao daoImpl) {
this.daoImpl = daoImpl;
}
}
Dao類及其實作類
public interface Dao {
void doSomething();
}
public class DaoImpl implements Dao {
@Override
public void doSomething() {
System.out.println("dao的doSomething方法!");
}
}
3、建立XML配置檔案
applicationContext.xml
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 每個Bean标簽對應一個Java類 -->
<!-- id保證唯一 calss:對應java類的全路徑 -->
<bean id="myservice" class="com.test.service.ServiceImpl" >
<!--每個property标簽對應一個依賴注入的屬性-->
<!-- name對應這個Java類對象的屬性名 ref:此屬性依賴注入的類 -->
<property name="daoImpl" ref="mydao"></property>
</bean>
<!--配置被依賴的類 -->
<!-- id保證唯一 calss:對應java類的全路徑 -->
<bean id="mydao" class="com.test.dao.DaoImpl">
<!--如果有需要,可以繼續嵌套依賴注入 -->
</bean>
</beans>
4、執行個體化核心對象 使用
//讀取配置檔案 執行個體化ApplicationContext對象
//參數為配置檔案xml的路徑
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//按照applicationContext.xml中的id擷取Service執行個體
Service ser=(Service) ac.getBean("myservice");
ser.doSomething();
運作結果:
我們沒有為Service的daoImpl屬性指派,但IOC為其注入了一個DaoImpl對象。
5、通過更改配置 更改依賴注入的類
假設因為業務需求,我們要使用另一個Dao。
新的Dao實作類
public class DaoImpl2 implements Dao {
@Override
public void doSomething() {
System.out.println("我是新的dao,我和之前的不一樣!");
}
}
我們不需要改變原有的代碼,隻需要修改配置檔案。
applicationContext.xml
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myservice" class="com.test.service.ServiceImpl" >
<property name="daoImpl" ref="mydao"></property>
</bean>
<!-- 唯一修改的地方 -->
<!-- 指向了DaoImple2-->
<bean id="mydao" class="com.test.dao.DaoImpl2">
</bean>
</beans>
運作結果
到這裡,我們 快速開始 的示範就結束了。
*6、其他
常用的getBean()的方式
//讀取配置檔案 執行個體化ApplicationContext對象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//1、按照xml配置檔案中的id擷取
Service ser=(Service) ac.getBean("myservice");
//2、按照類型擷取 要求xml中不能有相同類型的bean
Service ser2=ac.getBean(Service.class);
//3、結合上述兩種方法
Service ser3=ac.getBean("myservice", Service.class);
單例 和 多例
<!-- scope屬性: prototype為多例 singleton為單例 不加scope屬性預設單例 -->
<bean id="mydao" class="com.test.dao.DaoImpl2" scope="prototype">
</bean>
初始化 和 銷毀 動作
<!-- init-method 初始化時執行的方法 destroy-method 銷毀時執行的方法 -->
<bean id="mydao" class="此處略" init-method="init" destroy-method="close">
</bean>
*7、待完成
Spring IOC 注解方式 依賴注入
Spring IOC 加載XML檔案原理(待完成)