Spring IO Platform简介及示例

什么是Spring IO Platform

Spring IO Platform,简单的能够认为是一个依赖维护平台,该平台将相关依赖汇聚到一块儿,针对每一个依赖,都提供了一个版本号;html

这些版本对应的依赖都是通过测试的,能够保证一块儿正常使用。git

为何要使用Spring IO Platform

主要是解决依赖版本冲突问题,例如在使用Spring的时候,常常会使用到第三方库,通常你们都是根据经验挑选一个版本号或挑选最新的,随意性较大,其实这是有问题的,除非作过完整的测试,保证集成该版本的依赖不会出现问题,且后续集成其它第三方库的时候也不会出现问题,不然风险较大,且后续扩展会愈来愈困难,由于随着业务复杂度的增长,集成的第三方组件会越来会多,依赖之间的关联也会也来越复杂。github

好消息是,Spring IO Platform能很好地解决这些问题,咱们在添加第三方依赖的时候,不须要写版本号,它可以自动帮咱们挑选一个最优的版本,保证最大限度的扩展,并且该版本的依赖是通过测试的,能够完美的与其它组件结合使用。web

Spring IO Platform中维护了哪些依赖

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

完整的依赖列表请参考以下连接:apache

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

如何使用Spring IO Platform

Spring IO Platform主要是与依赖管理系统结合一块儿使用的,例如,能够完美的支持Maven和Gradle;maven

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

在Maven中使用Spring IO Platform

有两种方式,一种是使用import导入,另外一种是继承parent:spring-boot

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以外,咱们的应用还会引入一些插件管理的配置,如Spring Boot的Maven插件,咱们能够利用这一点,而后只须要在<plugins>代码块中添加以下代码便可使用插件:

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

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

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

若是你想结合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

参考资料

http://docs.spring.io/platform/docs/2.0.8.RELEASE/reference/htmlsingle/

相关文章
相关标签/搜索