Spring Boot参考文档(5)

原文链接:http://www.itbus.tech/detail.html?id=8718

真心希望之前的几篇文章对你能有一些帮助,让你对Spring Boot基本知识有了一些了解。如果你是一个任务驱动的开发者,你或许已经想跳过这些无聊的章节,直接开始了写代码,然后在项目中遇到遇到问题,解决问题。如果是这样,那你可以去看How-to参考指南。

Spring Boot的GitHub库有很多示例代码可以直接运行。这些示例代码都是独立的,你可以单独运行其中的某一个。

接下来,我们会开始使用Spring Boot。如果你真的已经没有耐心了,可以直接看Spring Boot特性

这一节我们会接触更多的关于我们如何去使用Spring Boot的细节。它包含了:构建工具,自动配置,如何运行。我们还会提及一些最佳实践。虽然对我们来说Spring Boot也只是一个Java的库,并没有什么特殊的,但是还是会有建议,如果遵守这些建议会让我们的开发更容易一些。

构建工具

强烈推荐你使用一个构建工具,来支持依赖管理,让你可以更方便的使用Maven仓库中的库。我们推荐你使用Maven或者Gradle。虽然也支持其他的构建工具(比如Ant),但是支持的没有那么完美。

依赖管理

Spring Boot的每个版本都会提供一个推荐的依赖列表。在实践中,对于这些依赖你不需要指定版本,因为Spring Boot会自己帮你选择最合适的版本。当你升级Spring Boot的时候,Spring Boot也会自动帮你升级这些依赖。

如果你觉得有必要,你也可以自己指定某个依赖的版本。

这个推荐的依赖列表,包含了所有你可能会用到的spring的各个模块,也包含一个精选的第三方库。这个列表作为一个材料清单是可以单独获取的。当你没有使用parent POM时,你需要自己来选择添加。

每个版本的Spring Boot都会有一个关联的最基本的Spring Framework的版本,对于这个依赖,我们强烈推荐你不要自己指定它的版本。

Maven

Maven用户可以依赖spring-boot-starter-parent来获得合适的默认配置。这个parent(父)项目提供了一下功能:

  • Java1.6作为默认的编译等级。
  • UTF-8作为文件编码。
  • 一个依赖推荐列表,让我们省略这些依赖的版本。
  • Maven的Filtering(Maven的一种过滤机制)。
  • 合理的插件配置(exec plugin,surefire,Git Commit ID,shade)。
  • 合理的配置文件过滤,如:application.propertiesapplication.yml,还有一些指定的文件(如:application-foo.propertiesapplication-foo.yml)。

最后指出一点,由于这些默认的配置文件都遵从Spring风格的占位符${...},所以Maven filtering被改成了@..@占位符(你可以通过设置resource.delimiter这个Maven属性来覆盖这一默认配置)。

继承starter parent

可以简单的让你的项目继承spring-boot-starter-parent

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

你只需要在这里指明Spring Boot的版本号,之后再添加其他starter的时候,就可以省略版本号了。

在这一步,你还可以覆盖某个依赖,通过添加一个属性。例如:为了升级另一个Spring Data release train,你可以这样来修改它的版本号:

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

spring-boot-dependencies查看支持的属性。

不继承parent POM

并不是每个人都愿意继承spring-boot-starter-parent的。你也许有你自己团队的parent去继承,又或者你就是更喜欢自己来定义所有的Maven配置。

即便如此,你也可以享受以来管理的福利(不是插件管理),通过使用scope=import:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

这样的话,就不支持设置属性来升级依赖版本,但是你可以在dependencyManagement里,在spring-boot-dependencies之前增加一个entry。举个例子:

<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.5.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

改变Java编译版本

spring-boot-starter-parent做出的是一个比较保守,但也比较通用的选择。如果你使用的事一个较新的版本,你可以增加一个java.version属性:

<properties>
    <java.version>1.8</java.version>
</properties>

使用Spring Boot Maven插件

Spring Boot包含了一个Maven插件来把我们的项目打包成一个可直接运行的jar包。把这个插件加到<plugins>中:

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

如果你是继承了Spring Boot的parent pom,你只需要加入这个插件就可以了,不需要做其他配置。除非你想改变parent中的一些默认配置。

Gradle

Gradle用户可以直接导入starter到他们的dependencies。不像Maven,这里没有parent的导入来共享一些配置。

repositories {
    jcenter()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.5.3.RELEASE")
}

spring-boot-gradle-plugin可以创建一个可执行的jar包,这个插件也提供了依赖管理。这样你也可以省略一些依赖的版本号:

plugins {
    id 'org.springframework.boot' version '1.5.3.RELEASE'
    id 'java'
}


repositories {
    jcenter()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Ant

你也有可能使用Ant+Ivy。spring-boot-antlib模块也可以帮助你创建一个可执行的jar包。

为了添加依赖,你可以这样做:

<ivy-module version="2.0">
    <info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
    <configurations>
        <conf name="compile" description="everything needed to compile this module" />
        <conf name="runtime" extends="compile" description="everything needed to run this module" />
    </configurations>
    <dependencies>
        <dependency org="org.springframework.boot" name="spring-boot-starter"
            rev="${spring-boot.version}" conf="compile" />
    </dependencies>
</ivy-module>

一个典型的build.xml

<project
    xmlns:ivy="antlib:org.apache.ivy.ant"
    xmlns:spring-boot="antlib:org.springframework.boot.ant"
    name="myapp" default="build">

    <property name="spring-boot.version" value="1.3.0.BUILD-SNAPSHOT" />

    <target name="resolve" description="--> retrieve dependencies with ivy">
        <ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
    </target>

    <target name="classpaths" depends="resolve">
        <path id="compile.classpath">
            <fileset dir="lib/compile" includes="*.jar" />
        </path>
    </target>

    <target name="init" depends="classpaths">
        <mkdir dir="build/classes" />
    </target>

    <target name="compile" depends="init" description="compile">
        <javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
    </target>

    <target name="build" depends="compile">
        <spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
            <spring-boot:lib>
                <fileset dir="lib/runtime" />
            </spring-boot:lib>
        </spring-boot:exejar>
    </target>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值