天天看点

使用Spring Boot的两种方式

继承spring-boot-starter-parent

如果继承spring-boot-starter-parent,需在pom.xml中加入如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M3</version>
</parent>
           
注:在此需要指定Spring Boot的版本号,如果要导入其他的starters,则可省略版本号。

在这种设置情况下,你也可以通过在自己的Pom覆盖一属性值的方式覆盖个别依赖。如,要升级spring data发布版:

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

不继承Spring Boot paren POM

并不是每个人都喜欢继承spring-boot-starter-parent,可能是因为有自己需要使用的企业父项目,或者你仅仅喜欢明确的声明你的所有MAVEN配置。

如果不想继承spring-boot-starter-parent,但想要使用其依赖管理可以以如下的方式使用:

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

使用此种方式就能像上面阐述的那样可以在自己项目Pom中改变一个属性的性即可覆盖个别依赖。为了达到这种效果,你需要在spring-boot-dependencies之前添加对应的依赖:

<dependencyManagement>
    <dependencies>
        <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>2.0.0.M3</version>
            <type>pom</type>
            <scope>import</scope>
        </denpendency>
    </dependencies>
</dependencyManagement>