Spring Boot依赖

使用Spring Boot很简单,先添加基础依赖包,有以下两种方式

1. 继承spring-boot-starter-parent项目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

2. 导入spring-boot-dependencies项目依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
</dependencyManagement>

Spring Boot依赖注意点

1. 属性覆盖只对继承有效

This only works if your Maven project inherits (directly or indirectly) from spring-boot-dependencies. If you have added spring-boot-dependencies in your own dependencyManagement section with import you have to redefine the artifact yourself instead of overriding the property.

Spring Boot依赖包里面的组件的版本都是和当前Spring Boot绑定的,如果要修改里面组件的版本,只需要添加如下属性覆盖即可,但这种方式只对继承有效,导入的方式无效。

<properties>
    <slf4j.version>1.7.25<slf4j.version>
</properties>

如果导入的方式要实现版本的升级,达到上面的效果,这样也可以做到,把要升级的组件依赖放到Spring Boot之前。

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

Each Spring Boot release is designed and tested against a specific set of third-party dependencies. Overriding versions may cause compatibility issues.

需要注意,要修改Spring Boot的依赖组件版本可能会造成不兼容的问题。

2. 资源文件过滤问题

使用继承Spring Boot时,如果要使用Maven resource filter过滤资源文件时,资源文件里面的占位符为了使${}和Spring Boot区别开来,此时要用@…@包起来,不然无效。另外,@…@占位符在yaml文件编辑器中编译报错,所以使用继承方式有诸多问题,坑要慢慢趟。

好了,今天的分享就到这里,更多 Spring Boot 文章正在撰写中,关注Java技术栈微信公众号获取第一时间推送。

在公众号后台回复:boot,还能获取栈长整理的往期 Spring Boot 教程,都是实战干货,以下仅为部分预览。

  • Spring Boot 读取配置的几种方式
  • Spring Boot 如何做参数校验?
  • Spring Boot 最核心的 25 个注解!
  • Spring Boot 2.x 启动全过程源码分析
  • Spring Boot 2.x 新特性总结及迁移指南
  • ……

本文原创首发于微信公众号:Java技术栈(id:javastack),转载请原样保留本信息。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注