1.為什麼學習Spring?
隨著對Java EE的不斷接觸和理解,你會發現Spring 在各個企業和項目中發揮著越來越重要的作用。掌握Spring 已成為我們IT行業生存必學的本領之一。
Spring Framework 當前最新版本是Spring Framework 5,當你打開官網,你應該能夠看到官網的宣傳圖片
這裡有個相關的新聞有興趣可以看下, 英文原版地址點擊查看中文版點擊查看
根據官網動態和我所了解的信息來看,Spring 官網還會繼續支持Spring MVC,因為它還有很多改進的地方。
但是未來的趨勢我認為必將是Spring Boot+ SpringWeb Flux + Spring Cloud .
那麼Spring MVC 和Spring Web Flux 兩者有何區別呢?
官網對此給出了這樣一張對比圖:
翻譯下就是:
總結:
看到這裡,相信此時聰明的你應該曉得為什麼我之前會那麼說了吧。
2. Spring官網介紹
Spring 官網: https://spring.io/ Spring
文檔: https://spring.io/guides
Spring IDE: https://spring.io/tools/sts
Spring Project: https://spring.io/projects
項目快速生成器: https://start.spring.io/
上面這些鏈接相信很多人都知道,但是其實往往我們不是很清楚什麼時候用哪個鏈接。
Spring 官網: 關注Spring 官網動態,最新的Spring 技術和版本發佈公告
Spring 文檔: what you want to do ? 你想開發一個什麼樣的項目?可以在這裡快速找到相關介紹和文檔。
Spring IDE: 如果你打算用Eclipse 版本,那麼我推薦用官網這個STS,因為它應該是最友好支持Spring的Eclipse 版本。當然,如果如果條件可以,我還是強烈推薦你使用Intellij Idea.
Spring Project: 這裡是按照項目模塊劃分的,比如從配置到安全,Web應用程序到大數據,想學習哪個就按照分類去學即可。
項目生成器:這裡是Spring 官網提供的一個非常便利的工具,需要哪些依賴,哪個版本,在這裡配置下然後下載即可。
3. Spring Framework
Spring Framework核心支持依賴注入,事務管理,Web應用程序,數據訪問,消息傳遞,測試和更多
Tips:這裡講述的是翻譯https://projects.spring.io/spring-framework/ 上面的內容
3.1 介紹
Spring框架為現代基於Java的企業應用程序提供了一個全面的編程和配置模型- 在任何類型的部署平台上。
Spring的一個關鍵元素是應用程序級別的基礎架構支持:Spring著重於企業應用程序的“管道”,以便團隊可以專注於應用程序級業務邏輯,而不必與特定部署環境形成不必要的聯繫。
3.2 功能特點
Tips:這裡加一張官網文檔中的一個截圖吧,相信有助於你更好地理解。
3.3 最低要求
Tips: 所以你的電腦現在推薦使用JDK1.8+
3.4 快速開始
在項目中開始使用spring-framework的推薦方法是使用依賴管理系統- 下面的代碼片段可以復制並粘貼到您的構建中。
需要幫忙? 請參閱我們有關使用Maven 和Gradle 構建的入門指南。
其實不止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;@Componentpublic 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@ComponentScanpublic 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 |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。