Spring 是什麼
Spring 是一個分層的 Java SE/EE 的輕量級框架,能夠提高開發人員的開發效率以及系統可維護性,官網地址:https://spring.io/。
Spring 的特徵
非侵入性
Spring 框架的 API 不會在業務邏輯上出現,業務邏輯可以從 Spring 框架快速地移植到其他框架,和環境無關。
容器
作爲一個容器,可以管理對象的生命週期、對象與對象的依賴關係,可以通過配置文件來定義對象並設置與其他對象的依賴關係。
IoC
IoC(Inverse of Control,控制反轉),既創建被調用者的實例不是由調用者完成,而是由 Spring 容器,並注入調用者。當應用了 IoC,一個對象依賴的其它對象會通過被動的方式傳遞進來,而不是這個對象自己創建或者查找依賴對象。即,不是對象從容器中查找依賴,而是容器在對象初始化時不等對象請求就主動將依賴傳遞給它。
AOP
通過分離應用的業務邏輯與系統級服務(例如日誌和事務管理)進行開發。應用對象只實現它們應該做的——完成業務邏輯。它們並不負責其它的系統級關注點,例如日誌或事務支持。
我們可以把日誌、安全、事務管理等服務理解成一個「切面」,那麼以前這些服務一直是直接寫在業務邏輯的代碼當中的,這有兩點不好:首先業務邏輯不純淨;其次這些服務被很多業務邏輯反覆使用,完全可以剝離出來做到複用。那麼 AOP 就是這些問題的解決方案, 可以把這些服務剝離出來形成一個「切面」,以期複用,然後將「切面」動態的「織入」到業務邏輯中,讓業務邏輯能夠享受到此「切面」的服務。
入門代碼
引入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mindartisan</groupId>
<artifactId>Spring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
創建 HelloSpring 類
package com.mindartisan.spring1;
public class HelloSpring {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("Hello,"+name);
}
}
resources 文件夾下新建配置文件
命名一般爲「applicationContext.xml」,代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<!-- 配置 Bean:其中,class 爲 bean 的全類名,通過反射的方式在 IOC 容器中創建 Bean,故要求 Bean 中有無參構造器;id 作用爲標識容器中的 bean,id 唯一。-->
<bean id="helloSpring" class="com.mindartisan.spring1.HelloSpring">
<!-- name 會調用類屬性的 set 方法-->
<property name="name" value="MindArtisan-Spring"></property>
</bean>
</beans>
創建一個類測試
傳統寫法
代碼:
package com.mindartisan.spring1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/**
* 傳統寫法
*/
// 1.實例一個 hellospring 對象
HelloSpring helloSpring=new HelloSpring();
// 2. 設置屬性
helloSpring.setName("MindArtisan——traditional");
// 3. 調用方法
helloSpring.hello();
}
}
輸出結果:
使用 Spring 後寫法
代碼:
package com.mindartisan.spring1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/**
* Spring 寫法
*/
// 1. 創建 Spring 的 IOC 容器
// 1.1 ApplicationContext 是一個接口,而 ClassPathXmlApplicationContext 是其實現類,具體可看 UML 圖
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 從 IOC 容器中獲取 Bean 實例
// 2.1 方式一:利用 id 定位到 IOC 容器中的 Bean
HelloSpring helloSpring = ((HelloSpring) applicationContext.getBean("helloSpring"));
// 2.2 方式二:利用類型返回 IOC 容器中的 Bean,要求是 IOC 容器中只有一個該類型的 Bean,否則代碼不知道應該返回哪個 Bean,不推薦使用
//HelloSpring helloSpring1 = applicationContext.getBean(HelloSpring.class);
// 3. 調用方法
helloSpring.hello();
}
}
輸出:
幾點思考
思考一: 這裏咱們沒有顯示地爲 name 賦值,咋就成 MindArtisan-Spring 了呢?
答案: 再看下 resources 文件夾下新建配置文件 中的內容即可明白。
思考二: applicationContext 爲我們做了什麼呢?
分析:
將 Main 類中的下述代碼註釋:
// 2. 從 IOC 容器中獲取 Bean 實例
// 2.1 方式一:利用 id 定位到 IOC 容器中的 Bean
// HelloSpring helloSpring = ((HelloSpring) applicationContext.getBean("helloSpring"));
// 2.2 方式二:利用類型返回 IOC 容器中的 Bean,要求是 IOC 容器中只有一個該類型的 Bean,否則代碼不知道應該返回哪個 Bean,不推薦使用
//HelloSpring helloSpring1 = applicationContext.getBean(HelloSpring.class);
// 3. 調用方法
// helloSpring.hello();
並將 HelloSpring 代碼改成下述內容:
package com.mindartisan.spring1;
public class HelloSpring {
private String name;
public void setName(String name) {
System.out.println("name 被設置爲:"+name);
this.name = name;
}
public void hello() {
System.out.println("Hello,"+name);
}
public HelloSpring() {
System.out.println("Constructor-HelloSpring");
}
}
輸出:
答案: 在創建 applicationContext 容器的時會調用構造器,將配置在配置文件中的 Bean 進行初始化,並調用 Bean 的 set 方法對屬性賦值。
關聯閱讀
Spring 中的 IoC、AOP 等內容正在不緊不慢地編寫中,敬請期待~