簡介:
Spring IO Platform是Spring官網中排第一位的項目。它將Spring的核心API集成到一個適用於現代應用程序的平台中。提供了Spring項目組合中的版本依賴。這些依賴關系是經過測試,可以保證正常工作。
為什么要使用?
Spring IO Platform主要是解決依賴版本的沖突問題。舉個栗子:在使用Spring的時候,經常會使用到第三方庫,一般大家都是根據經驗挑選一個版本浩或挑選最新的,其實這是存在隱患的。除非做過完整的測試,保證集成該版本的依賴不會出現問題,否則風險很大,且后續擴展會越來越困難。因為隨着業務復雜度的增加,集成的第三方組件會越來會多,依賴之間的關聯也會也來越復雜。
Spring IO Platform正好解決了這些問題,在我們添加第三方依賴時,不需要寫版本號,它能自動幫我們選擇一個最優的版本,保證最大限度的擴展。
維護了哪些依賴?
Spring IO Platform維護的依賴非常多,挑選了一些常見的(更多詳情請查看官網),如下表所示:
| Group | Artifact | Version |
|---|---|---|
| org.springframework.boot | spring-boot | 1.5.10.RELEASE |
| ch.qos.logback | logback-core | 1.1.11 |
| com.google.code.gson | gson | 2.8.2 |
| com.rabbitmq | amqp-client | 4.0.3 |
| com.rabbitmq | http-client | 1.1.1.RELEASE |
| junit | junit | 4.12 |
| org.apache.tomcat | tomcat-jdbc | 8.5.27 |
使用Spring IO Platform
Spring IO Platform主要用於管理系統依賴,可以支持Maven和Gradle。
在Maven中使用Spring IO Platform
Spring IO Platform支持import和繼承parent兩種方式:
import的方式:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 6 <modelVersion>4.0.0</modelVersion> 7 8 <groupId>com.example</groupId> 9 <artifactId>your-application</artifactId> 10 <version>1.0.0-SNAPSHOT</version> 11 12 <dependencyManagement> 13 <dependencies> 14 <dependency> 15 <groupId>io.spring.platform</groupId> 16 <artifactId>platform-bom</artifactId> 17 <version>Brussels-SR7</version> 18 <type>pom</type> 19 <scope>import</scope> 20 </dependency> 21 </dependencies> 22 </dependencyManagement> 23 <!-- Dependency declarations --> 24 </project>
繼承parent的方式:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <groupId>com.example</groupId> 7 <artifactId>your-application</artifactId> 8 <version>1.0.0-SNAPSHOT</version> 9 <parent> 10 <groupId>io.spring.platform</groupId> 11 <artifactId>platform-bom</artifactId> 12 <version>Brussels-SR7</version> 13 <relativePath/> 14 </parent> 15 <!-- Dependency declarations --> 16 </project>
采用繼承parent的方法,除了導入pom提供的依賴關系管理之外,應用程序還將獲得一些插件管理,為許多插件提供合理的默認設置,包括Spring Boot的Maven插件。 要利用這個默認配置,需要做的就是把這個插件包含在你的pom的<plugins>部分中:
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-maven-plugin</artifactId> 6 </plugin> 7 </plugins> 8 </build>
當想在自己的pom里添加了一個屬於Spring IO Platform中的依賴的時候,可以直接省略版本號,如下所示:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-core</artifactId> 5 </dependency> 6 </dependencies>
在Gradle中使用Spring IO Platform
如下所示,我們會應用io.spring.dependency-management這個插件,然后在dependencyManagement中導入bom
1 buildscript { 2 repositories { 3 jcenter() 4 } 5 dependencies { 6 classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE' 7 } 8 } 9 apply plugin: 'io.spring.dependency-management' 10 repositories { 11 mavenCentral() 12 } 13 dependencyManagement { 14 imports { 15 mavenBom 'io.spring.platform:platform-bom:Brussels-SR7' 16 } 17 }
當需要添加一個屬於Spring IO Platform中的依賴的時候,寫法與Maven類似,可以省略版本號,如下所示:
1 dependencies { 2 compile 'org.springframework:spring-core' 3 }
喜歡請微信掃描下面二維碼,關注我公眾號--“精修Java”,做一些實戰項目中的問題和解決方案分享。

