全面進入複習模式,從 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、什麼是控制反轉?
來自:https://www.cnblogs.com/chenssy/p/9576769.html 的解釋,我覺得說的非常通透,這裏引用過來:
- 誰控制誰:在傳統的開發模式下,我們都是採用直接 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、源碼地址
https://github.com/turoDog/review_spring
推薦閱讀: