2 springboot Starter

Starter使得編寫程序就像搭積木一樣,這也是springboot很火的一個重要原因吧。
Starter相當於模塊,包括:
1.這個模塊需要的依賴庫;
2.提供對模塊的配置項給使用者;
3.提供自動配置類對模塊內的Bean進行自動裝配;
我們在POM中引入starter,Spring Boot能自動掃描並加載相應的模塊。

1 starter原理

SpringBoot自動配置的原理:
第1步,SpringBoot 在啓動時會去依賴的starter包中尋
找 resources/META-INF/spring.factories 文件,然後根據文件中配置的Jar包去掃描項目所依賴的Jar包,這類似於 Java 的 SPI 機制。
第2步,根據 spring.factories配置加載AutoConfigure類。
第3步,根據 @Conditional註解的條件,進行自動配置並將Bean注入Spring Context 上下文當中。

2 starter的結構

以spring-boot-starter-redis爲例進行說明:
我們POM文件中引入spring-boot-starter-redis:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-redis</artifactId>
   <version>${spring-boot-starter-redis-version}</version>
</dependency>

我們可以看到spring-boot-starter-redis是一個空的工程,
在這裏插入圖片描述
spring-boot-starter-redis工程POM文件中引入了spring-data-redis的工程。

<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
</dependency>

spring-data-redis的工程包含了自動配置的類。
在這裏插入圖片描述

小結:springboot Starter包含starter project和自動配置project;

  •  starter project 是一個空的jar文件,僅提供輔助性依賴管理,會引用自動配置project
  •  自動配置project 按照自動註解實現自動配置類,在META-INF/spring.factories中配置自動配置的類。

3 自定義使用starter

本節建立一個project演示starter的使用,並輸出HellowWorld!
projec包含兩個module,starter module和webmodule。starter module包含了自動配置模類,web module依賴starter module。

3.1 starter module

第1步:POM中引入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId> 
</dependency>

第2步:定義配置的類:

@ConfigurationProperties(prefix = "starter")
public class StartProperties {
    private String config;
    public String getConfig() {
        return config;
    }
    public void setConfig(String config) {
        this.config = config;
    }
}

第3步: 定義一個service:

public class StartService {
    private StartProperties startProperties;
    public StartProperties getStartProperties() {
        return startProperties;
    }
    public void setStartProperties(StartProperties startProperties) {
        this.startProperties = startProperties;
    }
    public String print(){
        return  startProperties.getConfig();
    }
}

第4步:定義一個自動配置的類:

@Configuration
@ConfigurationProperties //web應用才生效
@EnableConfigurationProperties(StartProperties.class)
public class StartAutoConfiguration {
    @Autowired
    private StartProperties startProperties;
    @Bean
    public StartService helloService() {
        StartService startService = new StartService();
        startService.setStartProperties(startProperties);
        return startService;
    }
}

第5步:META-INF的spring.factories文件配置StartAutoConfiguration:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.feidao.starter.StartAutoConfiguration

3.2 web module

第1步:設置web module依賴starter module;
第2步:在application.properties 中加上配置:

starter.config=HelloWorld!

第3步:定義WebController類方便驗證:

@RestController
@RequestMapping(value = "/starter")
public class WebController {
    @Autowired
    private StartService startService;
    @RequestMapping(value = "/print")
    public String print(){
        String str = startService.print();
        return str;
    }
}

第4步:啓動web module,執行localhost:8080/starter/print驗證如下:
在這裏插入圖片描述

具體參考代碼:
https://github.com/alifeidao/springboot-simple chapter2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章