1. spring概述
- spring是一個開源框架
- spring為簡化企業級應用開發而生,解決的是業務邏輯層和其他各層的松耦合問題,他將面向接口的編程思想貫穿整個系統應用。
- spring是javaSE/EE的一站式框架。web層有spring-mvc,業務層有spring ioc、事務等機制,持久層提供了spring-jdbc或者直接整合現有的框架(mybatis)
1.1 spring的優點
- 方便解耦,簡化開發
- spring就是一個大工廠,可以將所有對象創建和依賴關系維護等工作交給spring管理。
- AOP編程的支持
- spring提供面向切面的編程,可以方便的實現對程序進行權限攔截、運行監控等功能。
- 聲明式事務的支持
- 只需要通過配置就可以完成對事務的管理,而無需手動編程。
- 方便程序的測試
- spring對junit4支持,可以通過注解方便的測試spring程序
- 方便集成各種優秀的框架
- spring不排斥各種優秀的開源框架,其內部提供了對各種優秀框架(如Structs、Hibernate、Mybatis等)的直接支持。
- 降低可javaEE api的使用難度
- spring對javaEE開發中非常難用的一些api(JDBC、遠程調用等)都進行了封裝,是這些api應用難度降低。
1.2 spring的模塊

2. spring IOC的引入

- 傳統的開發方式下,我們直接使用new,獲取相關的服務對象。
- 但是這樣不符合面向接口的編程思想(關於面向接口的好處,可以參考https://blog.csdn.net/qq376430645/article/details/9927225)。於是把接口和其對應的實現類分開,做到面向接口的編程模式。
- 但是這樣又不符合OCP原則(open-close原則),就是盡量不修改源碼做到對程序的擴展。於是引入了工廠模式,負責生成接口的實現類。做到接口和實現的解耦。
- 但是標准的工廠類依然會同接口和實現類耦合,故引入了一種新的設計模式:工廠模式+反射+配置文件,在工廠類中讀入同源碼無關的配置文件,使用反射的方式對實現類進行實例化,做到最終的解耦。bingo!
3. spring IOC helloworld
-
引入spring的依賴,直接引入context即可,因為會將其依賴的所有包全部引入。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.18.RELEASE</version> </dependency> -
實現簡單的bean類
-
UserService Interface
package com.ioc.demo1; public interface UserService { public void sayHello(); } -
UserService 實現類
package com.ioc.demo1; public class UserServiceImpl implements UserService { public void sayHello() { System.out.println("Hello Spring"); } }
-
-
編寫spring核心配置文件
-
在resources中創建xml配置文件application-context.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"> <!-- UserService的創建權交給spring --> <bean id="userService" class="com.ioc.demo1.UserServiceImpl"></bean> </beans>
-
-
在程序中讀取spring的配置文件,通過spring框架獲得bean,完成相應的操作
package com.ioc.demo1; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo1 { @Test /** * 傳統方式開發 */ public void demo1() { UserService userService = new UserServiceImpl(); userService.sayHello(); } @Test /** * spring的方式實現 */ public void demo2() { // spring工廠 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml"); // 通過工廠獲取類對象 UserService userService = (UserService) applicationContext.getBean("userService"); userService.sayHello(); } }
4. IOC和DI的基本概念
- IOC Inverse of Control 反轉控制的概念,就是將原本在程序中手動創建UserService對象的控制權,交由Spring框架管理。
- 簡單說,就是創建UserService對象控制權被反轉到了Spring框架。
- DI Dependency Injection 依賴注入的概念,就是在Spring創建這個對象的過程中,將這個對象所依賴的屬性注入進去。
關於DI我們這邊再實例講一下,我們的UserServiceImpl由於業務的需求,新增一個name的string字段。
package com.ioc.demo1;
public class UserServiceImpl implements UserService {
private String name;
public void sayHello() {
System.out.println("Hello Spring " + name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
由於該處的修改,在調用處需要做出相應的修改,首先UserService接口中沒有name字段,需要將接口類改成實現類,再加上屬性賦值的語句。如下所示:
@Test
/**
* 傳統方式開發,增加了name字段
*/
public void demo11() {
UserServiceImpl userService = new UserServiceImpl();
userService.setName("xxx");
userService.sayHello();
}
而spring開發模式中,由於DI的存在,我們只需要修改配置文件即可,在調用處無需修改任何代碼。
<bean id="userService" class="com.ioc.demo1.UserServiceImpl">
<property name="name" value="xxx"></property>
</bean>
