The spring Boot configuration file is detailed

Source: Internet
Author: User
Tags xmlns jcenter
Overview

We talked about how to build a simple spring boot application (see Spring Boot-Explore), where we learned how to configure the project, including system build, automatic configuration, Dependency injection, development tools, and so on, to make it work better. System Building

To facilitate dependency management, the authorities recommend that we use Maven or Gradle for dependency management. Of course, Spring Boot also supports other system building methods, such as Ant.
With spring boot, you don't need to provide a dependent version, and spring boot automatically manages it. When you perform a system upgrade, you only need to modify the spring boot version number, and spring boot automatically upgrades the other dependencies. Of course, you can also specify a dependent version to overwrite the recommended dependent version of Spring Boot, depending on your specific needs. Maven

We know that support for Spring boot can be added by inheriting Spring-boot-starter-parent, a parent project that provides us with the following default configurations: Java 1.6 Compilation level UTF-8 encoding relies on the management module, You can omit the <version> tag when adding dependencies, inherit from spring-boot-dependencies POM resource file filter (Will ${...} Variables defined in the output to the target file, replaced with the value of the variable) plug-in configuration (exec plugin, surefire, Git commit ID, shade) application.properties and application.yml File filtering

The POM project only supports single inheritance, but sometimes we have to inherit from other Pom, or use our own configuration. At this point, if we want to add support for Spring Boot, it can be done in other ways.

We can do dependency management by introducing spring-boot-dependencies and setting up Scope=import to let Spring boot: Listing 1-handing dependencies to spring boot management

<dependencyManagement>
     <dependencies>
        <dependency>
            <!--Import dependency Management from Spring Boot--
            <groupId>org.springframework.boot</groupId>
            <artifactid >spring-boot-dependencies</artifactId>
            <version>1.3.6.RELEASE</version>
            <type >pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

We can overwrite the dependent version provided by spring boot by introducing the dependent version we need before spring-boot-dependencies, for example: Listing 2-overwriting the dependencies provided by spring boot

<dependencyManagement> <dependencies> <!--Override Spring Data release train provided by Sprin G Boot-<dependency> <groupId>org.springframework.data</groupId> &
            Lt;artifactid>spring-data-releasetrain</artifactid> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</type> </dependency> <de Pendency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot -dependencies</artifactid> <version>1.3.6.RELEASE</version> <type>pom</ type> <scope>import</scope> </dependency> </dependencies> </dependen Cymanagement>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

To use the new Java compiler version, add a Java version of the property, as follows: Listing 3-Using a newer Java version

<properties>
    <java.version>1.8</java.version>
</properties>
1 2 3 1 2 3

To package the project into an executable file, you need to add the Spring-boot-maven-plugin plugin to <plugins>, as follows: Listing 4-Packaging the project as an executable jar

<build>
    <plugins>
        <plugin>
            <groupid>org.springframework.boot</groupid >
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins >
</build>
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Gradle

Unlike Maven, Gradle does not provide a corresponding parent project to share the configuration, only by introducing the relevant "starter POMs" dependency. The Spring-boot-gradle-plugin is used to package the project as an executable file, and the dependent dependencies are given to Spring boot for management, and the script for building the project is as follows: Code listing 5-gradle Building Project script

Buildscript {
    repositories {
        jcenter ()
    }

    dependencies {
        classpath ("Org.springframework.boot: Spring-boot-gradle-plugin:1.3.6.release ")
    }
}

Apply plugin: ' java '
apply plugin: ' Spring-boot '

repositories {
    jcenter ()
}

dependencies {
    compile ("Org.springframework.boot: Spring-boot-starter-web ")
    testcompile (" Org.springframework.boot:spring-boot-starter-test ")
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Ant

We can also build a spring boot project using Ant+ivy and use Spring-boot-antlib to help us create an executable jar.
The Ivy.xml is configured as follows: Code Listing 6-ivy.xml

<ivy-module version= "2.0" >
    <info organisation= "Org.springframework.boot" module= " Spring-boot-sample-ant "/>
    <configurations>
        <conf name=" Compile "description=" everything needed to compile the module "/>
        <conf name=" Runtime "extends=" compile "description=" everything needed to run T His module "/>
    </configurations>
    <dependencies>
        <dependency org=" Org.springframework.boot "Name=" Spring-boot-starter "
            rev=" ${spring-boot.version} "conf=" Compile "/>
    </dependencies>
</ivy-module>
1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11

The Build.xml is configured as follows: Code Listing 7-build.xml

<project xmlns:ivy= "antlib:org.apache.ivy.ant" xmlns:spring-boot= "Antlib:org.springframework.boot.ant" nam E= "MyApp" default= "Build" > <property name= "spring-boot.version" value= "1.3.0.build-snapshot"/> <tar Get Name= "resolve" description= "-Retrieve dependencies with Ivy" > <ivy:retrieve pattern= "lib/[conf]/[a Rtifact]-[type]-[revision]. [ext] "/> </target> 

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.