前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
在初學spring boot
時,官方示例中,都是讓我們繼承一個spring的?spring-boot-starter-parent?這個parent:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.1.RELEASE</version>
</parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
但是,一般情況下,在我們自己的項目中,會定義一下自己的 parent 項目,這種情況下,上面的這種做法就行不通了。那么,該如何來做呢?其實,在spring的官網也給出了變通的方法的:
在我們自己 parent 項目中,加下下面的聲明
?
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
請注意,它的?type?是?pom,scope?是?import,這種類型的?dependency?只能在?dependencyManagement?標簽中聲明。
?
?
?
然后,把我們項目中的 子項目 中,parent 的聲明,修改為我們自己項目的 parent 項目就可以了,比如,我的是:
<parent><groupId>org.test</groupId><artifactId>spring</artifactId><version>0.1-SNAPSHOT</version></parent>
?
有一點,需要注意一下。?
在 子項目 的?dependencies?中,不需要(也不能)再次添加對?spring-boot-dependencies?的聲明了,否則 子項目 將無法編譯通過。?
?
即,在 子項目 中,下面的配置是多余的:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId>
</dependency>
為什么會這個樣子呢??
因為?spring-boot-dependencies?根本就沒有對應的jar包,它只是一個?pom?配置,可以去?maven倉庫?看一下。?
它里面定義了?非常多?的依賴聲明。
所以,有了它之后,我們在 子項目 中使用到的相關依賴,就不需要聲明version了,如:
?
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>
如,spring-boot-starter-web
?和?spring-boot-starter-test
?在?spring-boot-dependencies
?中的聲明分別為:
?
?
?
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.5.1.RELEASE</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.1.RELEASE</version><exclusions><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions>
</dependency>
?
參考文檔?
-?spring 官方文檔
?
?