Spring Boot開啓的 2 種方式

Spring Boot依賴

使用Spring Boot很簡單,先添加基礎依賴包,有如下兩種方式spring

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 <scope>import</scope> 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以前。ide

<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

須要注意,要修改Spring Boot的依賴組件版本可能會形成不兼容的問題。.net

2. 資源文件過濾問題

使用繼承Spring Boot時,若是要使用Maven resource filter過濾資源文件時,資源文件裏面的佔位符爲了使${}和Spring Boot區別開來,此時要用@...@包起來,否則無效。另外,@...@佔位符在yaml文件編輯器中編譯報錯,因此使用繼承方式有諸多問題,坑要慢慢趟。code

推薦:Spring Boot & Cloud 最強技術教程繼承

掃描關注咱們的微信公衆號,乾貨天天更新。教程

image

相關文章
相關標籤/搜索