首先先回顧下module與maven/gradle的關係:java
模塊化的依賴關係,很容易讓人聯想到mven和gradle,這個在上篇中也說起,後來有讀者也提出module和maven是什麼關係?解答以下:web
Maven 有兩個主要的特徵:依賴管理和構建管理。 依賴管理便可以決定庫中的版本並從倉庫中下載下來。 構建管理即管理開發過程當中的任務,包括初始化、編譯、測試、打包等。spring
Module是系統內置用於表述組件之間的關係,對於版本的管理仍是處於最原始的狀體,管理一種強制的依賴關係。shell
總結一下:Maven仍是主要負責構建管理,Modules 對於像Maven這樣的構建工具(build tools)來講扮演的是輔助補充的角色。由於這些構建工具在構建時創建的依賴關係圖譜和他們版本能夠根據Module來建立,Module強制肯定了module和artifacts之間的依賴關係,而Maven對於依賴並不是是強制的。 具體可參考StackOverflow上的一篇問答:Project Jigsaw vs Maven on StackOverflowapache
說到starter,若是用過SpringBoot的開發者可能很熟悉,Starter主要用來簡化依賴用的。api
Starter主要用來簡化依賴用的tomcat
在Starter以前作MVC時要引入日誌組件,好比log4j,那麼須要找到依賴的log4j的版本,有的時候版本不匹配也會出現問題,由於咱們也不知道依賴什麼版本,那麼Starter就應運而生。springboot
咱們要在SpringBoot中引入WebMVC的支持時,咱們只需引入這個模塊spring-boot-starter-web,有了Starter以後,log4j就自動引入了,也不用關心版本這些問題,將這個模塊若是解壓包出來會發現裏面什麼都沒有,只定義了一些POM依賴,因此說Starter的做用主要是簡化依賴。微信
《Spring Boot in Action》
的附錄B列出了SpringBoot Starters的內容:mvc
Starter(Group ID: org.springframework.boot) | 傳遞依賴 |
---|---|
spring-boot-starter-web | org.springframework.boot:spring-boot-starter org.springframework.boot:spring-boot-starter-tomcat org.springframework.boot:spring-boot-starter-validation com.fasterxml.jackson.core:jackson-databind org.springframework:spring-web org.springframework:spring-webmvc |
spring-boot-starter-log4j2 | org.apache.logging.log4j:log4j-slf4j-impl org.apache.logging.log4j:log4j-api org.apache.logging.log4j:log4j-core org.slf4j:jcl-over-slf4j org.slf4j:jul-to-slf4j |
... | .... |
<!-- 依賴管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加依賴 -->
<dependencies>
<!-- 自動配置依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>1.5.2.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
複製代碼
package net.hellomypastor.springboot.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="dog.proterties")
public class DogProperties {
private String name;
private int age;
//set、get
}
複製代碼
package net.hellomypastor.springboot.starter;
public class DogService {
private DogProperties dogProperties;
public DogService() {
}
public DogService(DogProperties dogProperties) {
this.dogProperties = dogProperties;
}
public String getDogName() {
return dogProperties.getName();
}
public int getDogAge() {
return dogProperties.getAge();
}
}
複製代碼
package net.hellomypastor.springboot.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(DogProperties.class)
@ConditionalOnClass(DogService.class)
@ConditionalOnProperty(prefix="dog.proterties", value="enabled", matchIfMissing=true)
public class DogAutoConfiguration {
@Autowired
private DogProperties dogProperties;
@Bean
@ConditionalOnMissingBean(DogService.class)
public DogService personService() {
DogService dogService = new DogService(dogProperties);
return dogService;
}
}
複製代碼
org.springframework.boot.autoconfigure.EnableAutoConfiguration=net.hellomypastor.configure.DogAutoConfiguration
複製代碼
spring官方說明定義starter時最好是按
*-spring-boot-starter
的格式進行命名
mvn clean install
複製代碼
將上述starter做爲依賴,並配置application.properties:
dog.properties.name=dog2018
dog.properties.age=1
複製代碼
那麼在類中只要注入DogService就能獲取到配置的值:
package net.hellomypastor.springboot.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dog")
public class DogController {
@Autowired
private DogService dogService;
@GetMapping("name")
public String getDogName() {
return dogService.getDogName();
}
@GetMapping("age")
public int getDogAge() {
return dogService.getDogAge();
}
}
複製代碼
測試請求結果:
http:127.0.0.1:8080/starterdemo/dog/name
結果:dog2018
http:127.0.0.1:8080/starterdemo/dog/age
結果:1
複製代碼
好了,本期就講到這裏,下期咱們講講Java9中到另外一個新工具
JShell
,敬請期待~