日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Spring IO Platform简介及示例

發(fā)布時(shí)間:2025/3/15 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring IO Platform简介及示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

什么是Spring IO Platform

Spring IO Platform,簡單的可以認(rèn)為是一個(gè)依賴維護(hù)平臺(tái),該平臺(tái)將相關(guān)依賴匯聚到一起,針對(duì)每個(gè)依賴,都提供了一個(gè)版本號(hào);

這些版本對(duì)應(yīng)的依賴都是經(jīng)過測試的,可以保證一起正常使用。

為什么要使用Spring IO Platform

主要是解決依賴版本沖突問題,例如在使用Spring的時(shí)候,經(jīng)常會(huì)使用到第三方庫,一般大家都是根據(jù)經(jīng)驗(yàn)挑選一個(gè)版本號(hào)或挑選最新的,隨意性較大,其實(shí)這是有問題的,除非做過完整的測試,保證集成該版本的依賴不會(huì)出現(xiàn)問題,且后續(xù)集成其它第三方庫的時(shí)候也不會(huì)出現(xiàn)問題,否則風(fēng)險(xiǎn)較大,且后續(xù)擴(kuò)展會(huì)越來越困難,因?yàn)殡S著業(yè)務(wù)復(fù)雜度的增加,集成的第三方組件會(huì)越來會(huì)多,依賴之間的關(guān)聯(lián)也會(huì)也來越復(fù)雜。

好消息是,Spring IO Platform能很好地解決這些問題,我們?cè)谔砑拥谌揭蕾嚨臅r(shí)候,不需要寫版本號(hào),它能夠自動(dòng)幫我們挑選一個(gè)最優(yōu)的版本,保證最大限度的擴(kuò)展,而且該版本的依賴是經(jīng)過測試的,可以完美的與其它組件結(jié)合使用。

Spring IO Platform中維護(hù)了哪些依賴

詳細(xì)的就不列了,太多了,我這里截張圖示意下,如果你使用到以下依賴的話,那么可以不用聲明版本號(hào):

完整的依賴列表請(qǐng)參考如下鏈接:

http://docs.spring.io/platform/docs/current/reference/html/appendix-dependency-versions.html

如何使用Spring IO Platform

Spring IO Platform主要是與依賴管理系統(tǒng)結(jié)合一起使用的,例如,可以完美的支持Maven和Gradle;

下面,我們就分別來了解下在Maven和Gradle中如何使用Spring IO Platform;

在Maven中使用Spring IO Platform

有兩種方式,一種是使用import導(dǎo)入,另一種是繼承parent:

import方式:

<?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.example</groupId><artifactId>your-application</artifactId><version>1.0.0-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>Athens-SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>…<!-- Add Spring repositories --><!-- (you don't need this if you are using a .RELEASE version) --><repositories></repositories><pluginRepositories></pluginRepositories> </project>

繼承parent:

<?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.example</groupId><artifactId>your-application</artifactId><version>1.0.0-SNAPSHOT</version><parent><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>Athens-SR2</version><relativePath/></parent>…<!-- Add Spring repositories --><!-- (you don't need this if you are using a .RELEASE version) --><repositories></repositories><pluginRepositories></pluginRepositories> </project>

使用繼承的話,除了從父pom中引入Spring IO Platform之外,我們的應(yīng)用還會(huì)引入一些插件管理的配置,如Spring Boot的Maven插件,我們可以利用這一點(diǎn),然后只需要在<plugins>代碼塊中添加如下代碼即可使用插件:

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build>

另外,使用繼承的話,還可以直接覆蓋父類提供的依賴版本號(hào),如下所示:

<properties><foo.version>1.1.0.RELEASE</foo.version> </properties>

如果你想結(jié)合Spring IO Platform和Spring Boot一起使用的話,并不是一定要繼承Spring IO Platform POM,可以選擇使用導(dǎo)入的方式,然后自己將剩下的配置添加到POM里即可。有興趣可以參考Spring Boot參考指南的這一章節(jié)?using-boot-maven,會(huì)講述如何不用繼承方式來使用Spring Boot.

最后,要說的是,無論你使用哪種方式,都不會(huì)有任何依賴添加進(jìn)來;

當(dāng)你想在自己的pom里添加了一個(gè)屬于Spring IO Platform中的依賴的時(shí)候,可以直接省略版本號(hào),如下所示:

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency> </dependencies>

在Gradle中使用Spring IO Platform

如下所示,我們會(huì)應(yīng)用io.spring.dependency-management這個(gè)插件,然后在dependencyManagement中導(dǎo)入bom。

buildscript {repositories {jcenter()}dependencies {classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'} }apply plugin: 'io.spring.dependency-management'repositories {mavenCentral() }dependencyManagement {imports {mavenBom 'io.spring.platform:platform-bom:Athens-SR2'} }

當(dāng)需要添加一個(gè)屬于Spring IO Platform中的依賴的時(shí)候,寫法與Maven類似,可以省略版本號(hào),如下所示:

dependencies {compile 'org.springframework:spring-core' }

一個(gè)完整的示例,基于Maven, 結(jié)合Spring Boot

?示例的Pom文件如下:

<?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.example</groupId><artifactId>helloworld</artifactId><version>0.0.1-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>Athens-SR2</version><type>pom</type><scope>import</scope></dependency><dependency><!-- Import dependency management from Spring Boot --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.4.3.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- Additional lines to be added here... --><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>1.4.3.RELEASE</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

有幾點(diǎn)注意,這里我們沒有繼承Spring Boot的父Pom,也沒繼承Spring IO Platform的父POM,都是選擇導(dǎo)入的方式,所以使用spring-boot-maven-plugin插件的時(shí)候,就不能像上一篇那樣自動(dòng)繼承父POM的配置了,需要自己添加配置,綁定repackage?Goal;

另外,想你想要修改依賴版本號(hào)的時(shí)候,由于不是繼承,所以不能使用直接覆蓋properties屬性的方法,其實(shí)也很簡單,如果不想繼承Spring IO Platform中的依賴版本號(hào)的話,自己直接寫上版本號(hào)即可,Spring Boot的話,可采用如下方式,來對(duì)Spring Data release train進(jìn)行升級(jí)(注意要放在spring-boot-dependencies的前面):

<dependencyManagement><dependencies><!-- Override Spring Data release train provided by Spring Boot --><dependency><groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

最后,我們使用Gson庫做個(gè)測試,現(xiàn)在maven repository中維護(hù)的gson的最新版本是2.8,Spring IO Platform中維護(hù)的版本是2.7(有興趣可查閱appendix確認(rèn))。

然后當(dāng)我們開始構(gòu)建項(xiàng)目的時(shí)候,發(fā)現(xiàn)下載的gson版本確實(shí)是2.7。

轉(zhuǎn)載于:https://www.cnblogs.com/chenliyang/p/6542867.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Spring IO Platform简介及示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。