
文章已托管到GitHub,大家可以去GitHub查看閱讀,歡迎老板們前來Star! 搜索關注微信公眾號 碼出Offer 領取各種學習資料!
一、Spring概述
1.1 Web開發中的一些問題
- 傳統Web開發存在硬編碼所造成的過度程式耦合(例如:Service中作為屬性Dao物件)
- 部分Java EE API較為復雜,使用效率低(例如:JDBC開發步驟)
- 侵入性強,移植性差(例如:DAO實作的更換,從Connection到SqlSession)
1.2 什么是Spring?
- Spring 是一個開源的設計層面框架,它解決的是業務邏輯層和其他各層的松耦合問題,因此它將面向介面的編程思想貫穿整個系統應用
- Spring是眾多優秀設計模式的組合(工廠、單例、代理、配接器、包裝器、觀察者、模板、策略)
- Spring并未替代現有框架產品,而是將眾多框架進行有機整合,簡化企業級開發,俗稱"膠水框架"
1.3 官網
官方網站:https://spring.io/
GitHub:https://github.com/spring-projects
下載地址:http://repo.spring.io/release/org/springframework/spring/
1.4 Spring架構組成
Spring架構由諸多模塊組成,可分類為
- 核心技術:依賴注入 ,事件,資源,i18n,驗證,資料系結,型別轉換,SpEL,AOP
- 測驗:模擬物件,TestContext框架,Spring MVC測驗,WebTestClient,
- 資料訪問:事務 ,DAO支持,JDBC,ORM,封裝XML,
- Spring MVC和 Spring WebFlux Web框架,
- 集成:遠程處理,JMS,JCA,JMX,電子郵件,任務,調度,快取,
- 語言:Kotlin,Groovy,動態語言,
| Spring特點 |
|---|
![]() |
| Spring架構 |
![]() |
1.5 Spring依賴
| GroupId | ArtifactId | 說明 |
|---|---|---|
| org.springframework | spring-beans | Beans 支持,包含 Groovy |
| org.springframework | spring-aop | 基于代理的AOP支持 |
| org.springframework | spring-aspects | 基于AspectJ 的切面 |
| org.springframework | spring-context | 應用背景關系運行時,包括調度和遠程抽象 |
| org.springframework | spring-context-support | 支持將常見的第三方類別庫集成到 Spring 應用背景關系 |
| org.springframework | spring-core | 其他模塊所依賴的核心模塊 |
| org.springframework | spring-expression | Spring 運算式語言,SpEL |
| org.springframework | spring-instrument | JVM 引導的儀表(監測器)代理 |
| org.springframework | spring-instrument-tomcat | Tomcat 的儀表(監測器)代理 |
| org.springframework | spring-jdbc | 支持包括資料源設定和 JDBC 訪問支持 |
| org.springframework | spring-jms | 支持包括發送/接收JMS訊息的助手類 |
| org.springframework | spring-messaging | 對訊息架構和協議的支持 |
| org.springframework | spring-orm | 物件/關系映射,包括對 JPA 和 Hibernate 的支持 |
| org.springframework | spring-oxm | 物件/XML 映射(Object/XML Mapping,OXM) |
| org.springframework | spring-test | 單元測驗和集成測驗支持組件 |
| org.springframework | spring-tx | 事務基礎組件,包括對 DAO 的支持及 JCA 的集成 |
| org.springframework | spring-web | web支持包,包括客戶端及web遠程呼叫 |
| org.springframework | spring-webmvc | REST web 服務及 web 應用的 MVC 實作 |
| org.springframework | spring-webmvc-portlet | 用于 Portlet 環境的MVC實作 |
| org.springframework | spring-websocket | WebSocket 和 SockJS 實作,包括對 STOMP 的支持 |
| org.springframework | spring-jcl | Jakarta Commons Logging 日志系統 |
二、自定義工廠
撰寫一個讀取Spring組態檔并創建物件的工廠,了解Spring工廠創建物件的流程,以便使用自定義工廠來測驗Spring基本開發流程
package com.mylifes1110.factory;
import java.io.IOException;
import java.util.Properties;
/**
* @ClassName MyFactory
* @Description 自定義工廠(創建物件)
* @Author Ziph
* @Date 2020/7/12
* @Since 1.8
* @Version 1.0
*/
public class MyFactory {
private Properties properties = new Properties();
public MyFactory() {
}
public MyFactory(String config) throws IOException {
// 加載組態檔
properties.load(MyFactory.class.getResourceAsStream(config));
}
// 獲取物件
public Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
String classPath = properties.getProperty(beanName);
if (classPath != null) {
Class clazz = null;
clazz = Class.forName(classPath);
return clazz.newInstance();
}
return null;
}
}
三、Spring基本開發步驟
3.1 創建Maven專案
如果對Maven不了解的小伙伴可以參考Maven教程
| File -> NewProject |
|---|
![]() |
| 創建Maven專案 |
![]() |
3.2 引入依賴
在pom.xml檔案中引入Spring常用依賴
<dependencies>
<!-- Spring常用依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
3.3 創建Spring空組態檔
在resources檔案夾中創建一個名為
spring-context.xml,命名并無規定,還有其他的常用命名,比如:applicationContext.xml、beans.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">
</beans>
3.4 定義需要生產的Bean物件
定義一個Bean物件、生產該物件并測驗該物件內的方法(UserDaoImpl)
package com.mylifes1110.dao.impl;
import com.mylifes1110.bean.User;
import com.mylifes1110.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public int insertUser(User user) {
System.out.println("------insertUser and UserDao------");
return 0;
}
}
3.5 基本依賴注入
依賴注入在這里可以把它理解為將要生產的物件注入到Spring容器中,也就是在spring-context.xml檔案利用標簽注入,這樣就可以讓Spring知道你要生產的物件是誰
標簽: <bean id="唯一標簽" />
<?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="UserDao" class="com.mylifes1110.dao.impl.UserDaoImpl"/>
</beans>
3.6 呼叫Spring工廠創建物件
呼叫Spring工廠API介面ApplicationContext讀取配置Spring核心組態檔并創建工廠物件
package com.mylifes1110.dao;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserDaoImplTest {
/**
* @MethodName insertUser
* @Param []
* @Description 測驗使用Spring工廠獲取物件
* @Author Ziph
* @Date 2020/7/12
*/
@Test
public void insertUser() {
// 讀取組態檔所需創建物件中所需創建的bean物件并獲取spring工廠物件
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
// 通過注入時的唯一標識(id)獲取bean物件
UserDao userDao = (UserDao) context.getBean("UserDao");
// 使用物件
userDao.insertUser(null); // 輸出結果為:------insertUser and UserDao------
}
}
四、依賴與組態檔詳解
4.1 Spring的依賴關系
Spring框架包含多個模塊,每個模塊各司其職,可結合需求引入相關依賴Jar包實作功能,
注意: Jar包彼此存在依賴,只需引入最外層Jar即可由Maven自動將相關依賴Jar引入到專案中,
| Spring常用功能的Jar包依賴關系 |
|---|
![]() |
4.2 schema
組態檔中的頂級標簽中包含了語意化標簽的相關資訊(spring-context.xml頭檔案即是schema)
注意: Spring需要匯入的schema標簽是很有規律的,因為Spring是特別規范的,例如:如下context別名的schema標簽是我自己復制修改的,它也是我們所用到Spring的schema,你可以復制beans的schema來替換所有beans的關鍵字即可,但是記得復制全(注意查看三個有context標志標簽陳述句,另外“ : ”后面的是別名哦)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xsd">
</beans>
| schema名稱 | 描述 |
|---|---|
| xmlns | 語意化標簽所在的命名空間 |
| xmlns:xsi | XMLSchema-instance 標簽遵循Schema標簽標準 |
| xsi:schemaLocation | xsd檔案位置,用以描述標簽語意、屬性、取值范圍等 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/144556.html
標籤:Java





