1. 程式人生 > 程式設計 >spring boot微服務自定義starter原理詳解

spring boot微服務自定義starter原理詳解

這篇文章主要介紹了spring boot微服務自定義starter原理詳解,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

使用spring boot開發微服務後,工程的數量大大增加(一定要按照領域來切,不要一箇中間件客戶端包一個),讓各個jar從開發和執行時自包含成了一個重要的內容之一。spring boot starter就可以用來解決該問題(沒事啟動時別依賴於applicationContext.getBean獲取bean進行處理,依賴關係太折騰,有時候在複雜系統中解決此事比較麻煩,需要修改開源框架程式碼才能實現,反過來修改開源原始碼後,維護也是個麻煩事)。言歸正傳,說說自定義starter。首先請熟悉spring boot的核心理念,不然容易為了starter而starter,這種情況太多了。

建立一個maven專案,在pom檔案中新增如下依賴:

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.0.0.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

建立properties屬性類,用於讀取屬性(當然可選,如果一開始沒有按照spring boot autoconfig的套路來,改起來還是挺費勁的,但是一旦這麼做了,就會想,TMD這才是真正的開發模式,@Value那套早該丟了)。

@ConfigurationProperties(prefix = "com.xxx")
public class HelloServiceProperties {

  private String name = "james";

  private String hobby = "cc";

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getHobby() {
    return hobby;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
}

@ConfigurationProperties配置此註解可以自動匯入application.properties配置檔案中的屬性,前提需要指定屬性字首prefix。

3.建立配置類

public class HelloService {

  private String name;

  private String hobby;

  public String getName() {
    return "name is " + name;
  }

  public String getHobby() {
    return "hobby is " + hobby;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
}

4.建立自動配置類:

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.xxx",value = "enabled",matchIfMissing = true)@ComponentScan({"com.xxx"}) // 如果bean比較多,一般採用這種方式
public class HelloServiceAutoConfiguration {

  @Autowired
  private HelloServiceProperties helloServiceProperties;

  @Bean // bean比較少、且順序和邏輯有特殊要求的模組,一般採用這種方式
  @ConditionalOnMissingBean(HelloServiceConfiguration.class)
  public HelloServiceConfiguration helloServiceConfiguration() {
    HelloService helloService = new HelloService();
    helloService.setName(helloServiceProperties.getName());
    helloService.setHobby(helloServiceProperties.getHobby());
    return helloService;
  }
}

5.在resources資料夾下面新建一個META-INF檔案,並在下面建立spring.factories檔案,將4中的配置類進行註冊。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloServiceAutoConfiguration

6.新建一個springboot專案,在pom檔案中新增剛剛打包的jar的座標。

7.使用@Autowired訪問介面。

@SpringBootApplication
@RestController
public class Springboot03Application {

  @Autowired
  private HelloService helloService;

  public static void main(String[] args) {
    SpringApplication.run(Springboot03Application.class,args);
  }

  @RequestMapping("/name")
  public String getName() {
    return helloService.getName();
  }

  @RequestMapping("/hobby")
  public String getHobby() {
    return helloService.getHobby();
  }
}

相比原來要使用@Import註解匯入一個@Configuration類,或者在一處集中維護ComponentScan的所有路徑,使用autoconfigure starter可以讓應用明顯實現的更加自包含和解耦。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。