隨着對Java EE的不斷接觸和理解,你會發現Spring 在各個企業和項目中發揮着愈來愈重要的做用。掌握Spring 已成爲咱們IT行業生存必學的本領之一。html
- Spring Framework 是一個開源的Java/Java EE全功能棧(full-stack)的應用程序框架,以Apache許可證形式發佈,也有.NET平臺上的移植版本。
- 該框架基於 Expert One-on-One Java EE Design and Development(ISBN 0-7645-4385-7)一書中的代碼,最初由Rod Johnson和Juergen Hoeller等開發。
- Spring Framework提供了一個簡易的開發方式,這種開發方式,將避免那些可能導致底層代碼變得繁雜混亂的大量的屬性文件和幫助類。
Spring Framework 當前最新版本是Spring Framework 5,當你打開官網,你應該可以看到官網的宣傳圖片java
這裏有個相關的新聞有興趣能夠看下,英文原版地址點擊查看 中文版點擊查看react
根據官網動態和我所瞭解的信息來看,Spring 官網還會繼續支持Spring MVC,由於它還有不少改進的地方。git
可是將來的趨勢我認爲必將是 Spring Boot+ SpringWeb Flux + Spring Cloud.github
那麼Spring MVC 和 Spring Web Flux 二者有何區別呢?web
官網對此給出了這樣一張對比圖:spring
翻譯下就是:express
- Spring MVC基於servlet API構建,並使用一個同步阻塞I / O體系結構和一個單線程請求線程模型的Web 框架
- Spring WebFlux是一個非阻塞的Web框架,從該組創建起,利用多核,下一代處理器和大量併發鏈接。
總結:apache
看到這裏,相信此時聰明的你應該曉得爲何我以前會那麼說了吧。編程
上面這些連接相信不少人都知道,可是其實每每咱們不是很清楚何時用哪一個連接。
Spring 官網: 關注Spring 官網動態,最新的Spring 技術和版本發佈公告
Spring 文檔: what you want to do ? 你想開發一個什麼樣的項目?能夠在這裏快速找到相關介紹和文檔。
Spring IDE: 若是你打算用Eclipse 版本,那麼我推薦用 官網這個STS,由於它應該是最友好支持Spring的Eclipse 版本。固然,若是若是條件能夠,我仍是強烈推薦你使用Intellij Idea.
Spring Project: 這裏是按照項目模塊劃分的,好比 從配置到安全,Web應用程序到大數據,想學習哪一個就按照分類去學便可。
項目生成器:這裏是Spring 官網提供的一個很是便利的工具,須要哪些依賴,哪一個版本,在這裏配置下而後下載便可。
Spring Framework核心支持依賴注入,事務管理,Web應用程序,數據訪問,消息傳遞,測試和更多
Tips:這裏講述的是翻譯 https://projects.spring.io/spring-framework/ 上面的內容
Spring框架爲現代基於Java的企業應用程序提供了一個全面的編程和配置模型 - 在任何類型的部署平臺上。
Spring的一個關鍵元素是應用程序級別的基礎架構支持:Spring着重於企業應用程序的「管道」,以便團隊能夠專一於應用程序級業務邏輯,而沒必要與特定部署環境造成沒必要要的聯繫。
- 核心技術:依賴注入,事件,資源,i18n,驗證,數據綁定,類型轉換,SpEL,AOP。
- 測試:模擬對象,TestContext框架,Spring MVC測試,WebTestClient。
- 數據訪問:事務,DAO支持,JDBC,ORM,編組XML。
- Spring MVC和Spring WebFlux Web框架
- 整合:遠程處理,JMS,JCA,JMX,電子郵件,任務,調度,緩存。
- 語言:Kotlin,Groovy,動態語言。
Tips: 這裏加一張官網文檔中的一個截圖吧,相信有助於你更好地理解。
3.3 最低要求
Tips: 因此你的電腦如今推薦使用 JDK1.8+
在項目中開始使用spring-framework的推薦方法是使用依賴管理系統 - 下面的代碼片斷能夠複製並粘貼到您的構建中。
其實不止Spring 官網咱們現在的各大公司應該大多數也推薦咱們是用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 上下文的項目
準備工做:
Tips: 下載的時候有個坑注意下,若是你的JDK是64位,默認直接下載的STS是32位,會出現這個錯誤。
因此下載的時候必定要下載JDK匹配的版本才行,移步:https://spring.io/tools/sts/all
這裏下載完成後咱們在咱們的IDE空白處,右鍵——> New——> Other...
輸入maven 搜索,選擇Maven Project,建立一個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>
建立文件
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名字也行。
建立成功後項目結構應該是這樣
固然可能你會看着這種項目結構不舒服,那麼你也能夠選擇改變下。
項目結構就變成了這種:
運行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 |