Spring IO Platform introduction and example, springplatform

Source: Internet
Author: User

Spring IO Platform introduction and example, springplatform
What is Spring IO Platform?

Spring IO Platform is a simple dependency maintenance Platform that aggregates dependencies and provides a version number for each dependency;

The dependencies corresponding to these versions are tested and can be used properly together.

Why use Spring IO Platform?

It mainly solves dependency version conflicts. For example, a third-party library is often used when Spring is used. Generally, you can select a version number or the latest version number based on your experience, in fact, this is a problem. Unless a complete test is performed to ensure that the dependency for this version is not affected, and there will be no problems when other third-party libraries are integrated in the future. Otherwise, the risk is high, in addition, subsequent expansion will become more and more difficult, because as the business complexity increases, the number of third-party components integrated will increase, and the association between dependencies will become more and more complex.

The good news is that Spring IO Platform can solve these problems well. When adding third-party dependencies, we do not need to write version numbers. It can automatically help us select an optimal version, to maximize the scalability, and the dependency of this version is tested, it can be used perfectly with other components.

Which dependencies are maintained in Spring IO Platform?

The details are not listed, too much. I will show the figure below. If you use the following dependencies, you do not need to declare the version number:

For the complete dependency list, see the following link:

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

How to Use Spring IO Platform

Spring IO Platform is mainly used in combination with the dependency management system. For example, it can perfectly support Maven and Gradle;

Next, let's take a look at how to use Spring IO Platform in Maven and Gradle;

Use Spring IO Platform in Maven

There are two methods, one is to use import, and the other is to inherit the parent:

Import method:

<?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>

Inherit from 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>

In addition to introducing Spring IO Platform from the parent pom, our application also introduces some plug-in management configurations, such as the Maven plug-in of Spring Boot, which we can use, then you only need to add the following code to the <plugins> code block to use the plug-in:

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

In addition, if inheritance is used, the dependency version provided by the parent class can be directly overwritten, as shown below:

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

If you want to use Spring IO Platform and Spring Boot together, you do not have to inherit Spring IO Platform POM. You can use the import method, then add the remaining configuration to the POM. If you are interested, refer to the using-Boot-maven section in the Spring boot Reference Guide, which describes how to use Spring Boot without inheritance.

Finally, no dependency is added in any method;

When you want to add a dependency in Spring IO Platform to your pom, you can directly omit the version number, as shown below:

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-core</artifactId>    </dependency></dependencies>
Use Spring IO Platform in Gradle

As shown below, we will apply the plug-in io. spring. dependency-management, and thendependencyManagementImport 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'    }}

When you need to add a dependency in Spring IO Platform, the statement is similar to that in Maven, and the version number can be omitted, as shown below:

dependencies {    compile 'org.springframework:spring-core'}
A complete example, based on Maven, combined with Spring Boot

The example Pom file is as follows:

<?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>

Note: here we have not inherited the parent Pom of Spring Boot, nor the parent POM of Spring IO Platform. We chose the import method, therefore, when using the spring-boot-maven-plugin plug-in, you cannot automatically inherit the configuration of the parent POM as in the previous article. You need to add the configuration and bind the repackage Goal;

In addition, when you want to modify the dependency version number, because it is not an inheritance method, you cannot directly overwrite the properties attribute. In fact, it is very simple. If you do not want to inherit the dependency version number in Spring IO Platform, you can directly write the version number yourself. For Spring Boot, you can use the following method to upgrade Spring Data release train (before 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>

Finally, we use the Gson library for a test. The latest version of gson maintained in maven repository is 2.8, and the version maintained in Spring IO Platform is 2.7 (For details, refer to appendix ).

Then, when we started building the project, we found that the downloaded gson version was indeed 2.7.

Sample source code

Https://github.com/peterchenhdu/helloworld

References

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.