Spring Framework 5.0 入門教程
1. 為什么學習Spring?
隨著對Java EE的不斷接觸和理解,你會發(fā)現(xiàn)Spring 在各個企業(yè)和項目中發(fā)揮著越來越重要的作用。掌握Spring 已成為我們IT行業(yè)生存必學的本領之一。
- Spring Framework 是一個開源的Java/Java EE全功能棧(full-stack)的應用程序框架,以Apache許可證形式發(fā)布,也有.NET平臺上的移植版本。
- 該框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-7645-4385-7)一書中的代碼,最初由Rod Johnson和Juergen Hoeller等開發(fā)。
- Spring Framework提供了一個簡易的開發(fā)方式,這種開發(fā)方式,將避免那些可能致使底層代碼變得繁雜混亂的大量的屬性文件和幫助類。
Spring Framework 當前最新版本是Spring Framework 5,當你打開官網,你應該能夠看到官網的宣傳圖片

這里有個相關的新聞有興趣可以看下, 英文原版地址點擊查看 中文版點擊查看
根據(jù)官網動態(tài)和我所了解的信息來看,Spring 官網還會繼續(xù)支持Spring MVC,因為它還有很多改進的地方。
但是未來的趨勢我認為必將是 Spring Boot+ SpringWeb Flux + Spring Cloud .
那么Spring MVC 和 Spring Web Flux 兩者有何區(qū)別呢?
官網對此給出了這樣一張對比圖:

翻譯下就是:
- Spring MVC基于servlet API構建,并使用一個同步阻塞I / O體系結構和一個單線程請求線程模型的Web 框架
- Spring WebFlux是一個非阻塞的Web框架,從該組建立起,利用多核,下一代處理器和大量并發(fā)連接。
總結:
- Spring MVC 是基于Servlet API 構建的同步阻塞式I/O 的Web 框架。
- Spring WebFlux 是一種更優(yōu)秀的非阻塞式Web框架,而且能更好處理大量并發(fā)連接。
看到這里,相信此時聰明的你應該曉得為什么我之前會那么說了吧。
2. Spring 官網介紹
Spring 官網: https://spring.io/ Spring
Spring IDE: https://spring.io/tools/sts
Spring Project: https://spring.io/projects
項目快速生成器: https://start.spring.io/
上面這些鏈接相信很多人都知道,但是其實往往我們不是很清楚什么時候用哪個鏈接。
Spring 官網: 關注Spring 官網動態(tài),最新的Spring 技術和版本發(fā)布公告
Spring 文檔: what you want to do ? 你想開發(fā)一個什么樣的項目?可以在這里快速找到相關介紹和文檔。

Spring IDE: 如果你打算用Eclipse 版本,那么我推薦用 官網這個STS,因為它應該是最友好支持Spring的Eclipse 版本。當然,如果如果條件可以,我還是強烈推薦你使用Intellij Idea.
Spring Project: 這里是按照項目模塊劃分的,比如 從配置到安全,Web應用程序到大數(shù)據(jù),想學習哪個就按照分類去學即可。

項目生成器:這里是Spring 官網提供的一個非常便利的工具,需要哪些依賴,哪個版本,在這里配置下然后下載即可。
3. Spring Framework
Spring Framework核心支持依賴注入,事務管理,Web應用程序,數(shù)據(jù)訪問,消息傳遞,測試和更多
Tips:這里講述的是翻譯 https://projects.spring.io/spring-framework/ 上面的內容
3.1 介紹
Spring框架為現(xiàn)代基于Java的企業(yè)應用程序提供了一個全面的編程和配置模型 - 在任何類型的部署平臺上。
Spring的一個關鍵元素是應用程序級別的基礎架構支持:Spring著重于企業(yè)應用程序的“管道”,以便團隊可以專注于應用程序級業(yè)務邏輯,而不必與特定部署環(huán)境形成不必要的聯(lián)系。
3.2 功能特點
- 核心技術:依賴注入,事件,資源,i18n,驗證,數(shù)據(jù)綁定,類型轉換,SpEL,AOP。
- 測試:模擬對象,TestContext框架,Spring MVC測試,WebTestClient。
- 數(shù)據(jù)訪問:事務,DAO支持,JDBC,ORM,編組XML。
- Spring MVC和Spring WebFlux Web框架
- 整合:遠程處理,JMS,JCA,JMX,電子郵件,任務,調度,緩存。
- 語言:Kotlin,Groovy,動態(tài)語言。
Tips:這里加一張官網文檔中的一個截圖吧,相信有助于你更好地理解。

3.3 最低要求
- JDK 8+ for Spring Framework 5.x
- JDK 6+ for Spring Framework 4.x
- JDK 5+ for Spring Framework 3.x
Tips: 所以你的電腦現(xiàn)在推薦使用 JDK1.8+
3.4 快速開始
在項目中開始使用spring-framework的推薦方法是使用依賴管理系統(tǒng) - 下面的代碼片段可以復制并粘貼到您的構建中。
需要幫忙? 請參閱我們有關使用 Maven 和 Gradle 構建的入門指南。
其實不止Spring 官網我們如今的各大公司應該大多數(shù)也推薦我們是用Maven和Gradle 來管理項目Jar包依賴。
如果你使用的Maven:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId> spring-context</artifactId>
<version>5.1.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies><repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
如果你是用的是Gradle
dependencies {
compile 'org.springframework: spring-context:5.1.0.BUILD-SNAPSHOT'
}repositories {
maven {
url 'https://repo.spring.io/libs-snapshot'
}
}
Tips: 其實我覺得Gradle應該是一種比Maven更先進的版本依賴管理工具,不過如今各大公司使用Gradle 似乎還不是很多,也許是與因為Eclipse 對Gradle 的支持還不夠像Intellij Idea 那么完美吧。
Spring框架包含許多不同的模塊。 這里我們展示了提供核心功能的spring-context。 有關其他選項,請參閱右側的入門指南。
一旦你使用spring-context依賴關系設置你的構建,你就可以做到以下幾點:
到這里后官網有些不詳細,補充下。
方法一: 使用STS 工具構建這個帶有Spring-context 上下文的項目
準備工作:
- JDK 1.8
- STS IDE
Tips: 下載的時候有個坑注意下,如果你的JDK是64位,默認直接下載的STS是32位,會出現(xiàn)這個錯誤。

所以下載的時候一定要下載JDK匹配的版本才行,移步: https://spring.io/tools/sts/all

這里下載完成后我們在我們的IDE空白處,右鍵——> New——> Other...

輸入maven 搜索,選擇Maven Project,創(chuàng)建一個Maven項目

選擇默認的工作空間

選擇默認的類型

輸入項目基本信息后點擊Finish 完成

然后我們應該可以看到這樣的項目結構

首先修改pom.xml
<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.xingyun</groupId>
<artifactId>spring-context-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-context-sample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId> spring-context</artifactId>
<version>5.1.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
創(chuàng)建文件
hello/MessageService.java
package com.xingyun.spring_context_sample.hello;
public interface MessageService {
String getMessage();
}
hello/MessagePrinter.java
package com.xingyun.spring_context_sample.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
final private MessageService service;
@Autowired
public MessagePrinter(MessageService service) {
this.service = service;
}
public void printMessage() {
System.out.println(this.service.getMessage());
}
}
Tips: 注意下這里有個注解不要忘了
App.java
package com.xingyun.spring_context_sample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.xingyun.spring_context_sample.hello.MessagePrinter;
import com.xingyun.spring_context_sample.hello.MessageService;
@Configuration
@ComponentScan
public class App
{
@Bean
MessageService mockMessageService() {
return new MessageService() {
public String getMessage() {
return "Hello World!";
}
};
}
public static void main( String[] args )
{
ApplicationContext context =
new AnnotationConfigApplicationContext(App.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
Tips: 注意類上有兩個注解和方法上有一個注解不要忘了,類的名字你可以改成官網上的Application 也可以保留默認的App名字也行。
創(chuàng)建成功后項目結構應該是這樣

當然可能你會看著這種項目結構不舒服,那么你也可以選擇改變下。

項目結構就變成了這種:

運行App.main() 主方法

項目源碼下載: https://github.com/geekxingyun/JavaEE-Framework-Sample/tree/master/spring-context-sample
附錄:核心Jar包依賴關系
Tips: 如果你不是寫一個java web Application,那么將不需要spring-web 模塊。
GroupId |
ArtifactId |
Description |
|
org.springframework |
spring-aop |
Proxy-based AOP support |
|
org.springframework |
spring-aspects |
AspectJ based aspects |
|
org.springframework |
spring-beans |
Beans support, including Groovy |
|
org.springframework |
spring-context |
Application context runtime, including scheduling and remoting abstractions |
|
org.springframework |
spring-context-support |
Support classes for integrating common third-party libraries into a Spring application context |
|
org.springframework |
spring-core |
Core utilities, used by many other Spring modules |
|
org.springframework |
spring-expression |
Spring Expression Language (SpEL) |
|
org.springframework |
spring-instrument |
Instrumentation agent for JVM bootstrapping |
|
org.springframework |
spring-instrument-tomcat |
Instrumentation agent for Tomcat |
|
org.springframework |
spring-jdbc |
JDBC support package, including DataSource setup and JDBC access support |
|
org.springframework |
spring-jms |
JMS support package, including helper classes to send/receive JMS messages |
|
org.springframework |
spring-messaging |
Support for messaging architectures and protocols |
|
org.springframework |
spring-orm |
Object/Relational Mapping, including JPA and Hibernate support |
|
org.springframework |
spring-oxm |
Object/XML Mapping |
|
org.springframework |
spring-test |
Support for unit testing and integration testing Spring components |
|
org.springframework |
spring-tx |
Transaction infrastructure, including DAO support and JCA integration |
|
org.springframework |
spring-web |
Foundational web support, including web client and web-based remoting |
|
org.springframework |
spring-webmvc |
HTTP-based Model-View-Controller and REST endpoints for Servlet stacks |
|
org.springframework |
spring-webmvc-portlet |
MVC implementation to be used in a Portlet environment |
|
org.springframework |
spring-websocket |
WebSocket and SockJS infrastructure, including STOMP messaging support |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringSecurity 手機號登錄功能實現(xiàn)
這篇文章主要介紹了SpringSecurity 手機號登錄功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2023-12-12
idea perttier的使用和縮進改為4不成功問題及解決
這篇文章主要介紹了idea perttier的使用和縮進改為4不成功問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
解決bufferedReader.readLine()讀到最后發(fā)生阻塞的問題
這篇文章主要介紹了解決bufferedReader.readLine()讀到最后發(fā)生阻塞的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

