Spring 覆盤 | IOC
全面進入複習模式,從 Spring 開始。
1、關於 Spring
Spring 是一個輕量級的開源框架,是為解決企業應用開發的複雜性而建立的。我很不喜歡這種略顯官方的說辭。千人千面,每個人對技術的理解都不一樣。而在我的理解中,Spring 的主要就解決了兩件事情(當然它還解決了資料訪問、遠端呼叫、單元測試等問題),分別對應 Spring 的兩個設計思想 IOC 和 AOP:
- IOC 容器(解耦合):解決各種 new 物件的問題
- AOP (切面程式設計):把非業務範疇的功能,提取成一個切面,統一實現
2、Spring 概覽
Spring 框架分為 6 個模組, 20+ 個 jar 包。為此我做了一張思維導圖,如下:
圖片可能不太清晰,1080 高清無碼 Spring 思維導圖獲取地址:Spring概覽
3、什麼是 IOC ?
IoC 全稱為 Inversion of Control,翻譯為 “控制反轉”,它還有一個別名為 DI(Dependency Injection),即依賴注入。說白了,IOC 就是由 Spring IOC 容器來負責物件的生命週期和物件之間的關係。
4、什麼是控制反轉?
來自:www.cnblogs.com/chenssy/p/9… 的解釋,我覺得說的非常通透,這裡引用過來:
- 誰控制誰:在傳統的開發模式下,我們都是採用直接 new 一個物件的方式來建立物件,也就是說你依賴的物件直接由你自己控制,但是有了 IOC 容器後,則直接由 IoC 容器來控制。所以“誰控制誰”,當然是 IoC 容器控制物件。
- 控制什麼:控制物件。
- 為何是反轉:沒有 IoC 的時候我們都是在自己物件中主動去建立被依賴的物件,這是正轉。但是有了 IoC 後,所依賴的物件直接由 IoC 容器建立後注入到被注入的物件中,依賴的物件由原來的主動獲取變成被動接受,所以是反轉。
- 哪些方面反轉了:所依賴物件的獲取被反轉了。
5、直接 new 物件
public class StudentService {
public static void main(String args[]) {
StudentDao studentDao = new StudentDao();
System.out.println(studentDao.save());
}
}
複製程式碼
在沒有 ioc 之前,我們都是直接 new 所依賴的物件,這是的控制權在於程式設計師。
6、IOC 依賴注入
IOC 依賴注入,分以下 3 種方式注入:
- 構造器注入
- setter 方法注入
- 介面方式注入
其中介面方式注入用的很少,此文不再討論。首先建立兩個類,StudentService、StudentDao 。其中 StudentService 依賴 StudentDao。
xml 構造器注入
StudentDao 程式碼:
public class StudentDao {
public String save(){
return"儲存學生資訊";
}
}
複製程式碼
StudentService 程式碼:
public class StudentService {
private StudentDao studentDao;
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
public String save(){
return studentDao.save();
}
}
複製程式碼
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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="StudentService">
<constructor-arg ref="studentDao"/>
</bean>
<bean id="studentDao" class="StudentDao"/>
</beans>
複製程式碼
測試方法:
public static void main(String srgs[]) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xmlConfig.xml");
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
System.out.println(studentService.save());
}
複製程式碼
resource 目錄下新建 xmlConfig.xml 檔案,引入 Spring xsd 規範,不瞭解 xsd 的自行百度。配置兩個 bean :StudentService、StudentDao 前者依賴後者,依賴方式是建構函式, 注意到 xml 中的 constructor-arg 標籤,表明了這一點。同時,它的屬性 ref 指向了 StudentDao 中的 id 。意思就是把 StudentDao 當做引數傳給 StudentService 的構造方法。
xml set 方法注入
StudentDao 程式碼不變,StudentService 程式碼如下:注入 StudentDao 方式變成 set 方法
public class StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public String save(){
return studentDao.save();
}
}
複製程式碼
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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="StudentService">
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="studentDao" class="StudentDao"/>
</beans>
複製程式碼
測試方法:
public static void main(String srgs[]) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xmlConfig.xml");
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
System.out.println(studentService.save());
}
複製程式碼
resource 目錄下新建 xmlConfig.xml 檔案,引入 Spring xsd 規範,不瞭解 xsd 的自行百度。配置兩個 bean :StudentService、StudentDao 前者依賴後者,依賴方式是set 方法, 注意到 xml 中的 property 標籤,表明了這一點。同時,它的屬性 ref 指向了 StudentDao 中的 id 。意思就是把 StudentDao 當做引數傳給 StudentService 的 set 方法。
javaConfig 注入
StudentDao 不變,StudentService 程式碼如下:
public class StudentService {
private StudentDao studentDao;
@Autowired
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
public String save() {
return studentDao.save();
}
}
複製程式碼
配置類:
/**
* 宣告這是一個配置類,程式執行時初始化這個類,把 @Bean 註解的 bean 載入到 ioc 容器備用
*/
@Configuration
public class StudentConfig {
@Bean
public StudentDao studentDao() {
return new StudentDao();
}
@Bean
public StudentService studentService(StudentDao studentDao) {
return new StudentService(studentDao);
}
}
複製程式碼
測試方法:
public class App {
public static void main(String srgs[]) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig.class);
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
System.out.println(studentService.save());
}
}
複製程式碼
注意到這裡新增了個 StudentConfig 這個類是一個配置類,加上 @Configuration 註解之後,程式啟動時就會把帶 @Bean 註解的 bean 載入到 ioc 容器中備用。StudentService 類的 set 方法上加了一個 @Autowired 註解,意思是按照類名載入 StudentDao 。
自動裝配
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- base-package 指定掃描包 -->
<context:component-scan base-package="student" />
</beans>
複製程式碼
StudentDao 加上 @Component 讓自動掃描發現
@Component
public class StudentDao {
public String save(){
return"儲存學生資訊";
}
}
複製程式碼
StudentService 加上 @Component 讓自動掃描發現
@Component
public class StudentService {
private StudentDao studentDao;
@Autowired
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
public String save() {
return studentDao.save();
}
}
複製程式碼
測試方法
public class StudentTest {
@Test
public void testSave() {
ApplicationContext context = new ClassPathXmlApplicationContext("autoConfig.xml");
StudentService studentService = (StudentService) context.getBean("studentService",StudentService.class);
Assert.assertNotNull(studentService.save());
}
}
複製程式碼
注意到 xml 的頭部宣告多了一些 url ,那是因為自動掃描標籤是在 context 包下管理的。使用他,必須加入 context 名稱空間。
7、原始碼地址
推薦閱讀: