Spring IO Platform简介及示例

9 篇文章 0 订阅
6 篇文章 0 订阅

目录

为什么要用Spring IO Platform

什么是Spring IO Platform

Maven使用Spring IO Platform

Spring IO Platform中维护了哪些依赖

如何使用Spring IO Platform

在Maven中使用Spring IO Platform

在Gradle中使用Spring IO Platform

一个完整的示例,基于Maven, 结合Spring Boot

示例源码


 

为什么要用Spring IO Platform

今天无意间看到了一个关键词:”Spring IO Platform”,第一直觉是不是有关于IO方面的框架或者包呢,查了一下,居然是为了解决jar包冲突而生的。Java Web开发中,经常遇到问题,编译没有报错,启动时报一些奇奇怪怪的问题,90%的都是包冲突导致的,虽然有一些包冲突解决插件,比如”Dependency Analyzer”等,也能很容易解决,但是如果能防范于未然,那是多么美好的一件事情。
好了,废话不多说了,简单介绍一下这个包吧。

什么是Spring IO Platform

Spring IO Plat是一个附带包,不会编译到项目中,它只是将核心Spring API框架内聚集成到一个应用程序的平台中。它提供了已经测试完毕能很好协同工作的许多项目的Spring组合版本以及它们的依赖项。

Spring IO Platform,简单的可以认为是一个依赖维护平台,该平台将相关依赖(核心Spring API框架)汇聚到一起,针对每个依赖,都提供了一个版本号;这些版本对应的依赖都是经过测试的,可以保证一起正常使用。

举个例子说,如果你的包引入了spring-boot-starter-web和spring-boot-starter-test这两个包,不用手动填写包的版本,Spring IO Plat会下载这两个包最适合的版本下来,避免因为版本的问题,导致的这样那样的问题。

Maven使用Spring IO Platform

我平时用的是Maven来继承依赖包,所以这里介绍一下Maven怎么使用这个包,有两种使用方式,一种是直接在依赖里引入Spring IO Platform,另外一种方式通过依赖继承Spring IO Platform。

Spring IO Platform中维护了哪些依赖

详细的就不列了,太多了,我这里截张图示意下,如果你使用到以下依赖的话,那么可以不用声明版本号:

完整的依赖列表请参考如下链接:

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

如何使用Spring IO Platform

Spring IO Platform主要是与依赖管理系统结合一起使用的,例如,可以完美的支持Maven和Gradle;

下面,我们就分别来了解下在Maven和Gradle中如何使用Spring IO Platform;

在Maven中使用Spring IO Platform

有两种方式,一种是使用import导入(官方推荐),另一种是继承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>

另外,使用继承的话,还可以直接覆盖父类提供的依赖版本号,如下所示:

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

使用继承的话,不只是依赖包,插件也是可以用这种方式确定和依赖想匹配的组合的,比如Spring Boot的Maven插件,我们可以利用这个特性,在代码块中添加如下代码即可使用合适版本的插件:

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

如果你想结合Spring IO Platform和Spring Boot一起使用的话,并不是一定要继承Spring IO Platform POM,可以选择使用导入的方式,然后自己将剩下的配置添加到POM里即可。有兴趣可以参考Spring Boot参考指南的这一章节 using-boot-maven,会讲述如何不用继承方式来使用Spring Boot.

最后,要说的是,无论你使用哪种方式,都不会有任何依赖添加进来;

当你想在自己的pom里添加了一个属于Spring IO Platform中的依赖的时候,可以直接省略版本号,如下所示:

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

在Gradle中使用Spring IO Platform

如下所示,我们会应用io.spring.dependency-management这个插件,然后在dependencyManagement中导入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'
    }
}

 

当需要添加一个属于Spring IO Platform中的依赖的时候,写法与Maven类似,可以省略版本号,如下所示:

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

一个完整的示例,基于Maven, 结合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>

 

注意一:这里我们没有继承Spring Boot的父Pom,也没继承Spring IO Platform的父POM,都是选择导入的方式,所以使用spring-boot-maven-plugin插件的时候,就不能像上一篇那样自动继承父POM的配置了,需要自己添加配置,绑定repackage Goal;

注意二:想你想要修改依赖版本号的时候,由于不是继承,所以不能使用直接覆盖properties属性的方法,其实也很简单,如果不想继承Spring IO Platform中的依赖版本号的话,自己直接写上版本号即可,Spring Boot的话,可采用如下方式,来对Spring Data release train进行升级(注意要放在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库做个测试,现在maven repository中维护的gson的最新版本是2.8,Spring IO Platform中维护的版本是2.7(有兴趣可查阅appendix确认)。

然后当我们开始构建项目的时候,发现下载的gson版本确实是2.7。

示例源码

 https://github.com/peterchenhdu/helloworld

参考文件:

https://www.cnblogs.com/xiang--liu/p/9710166.html

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值