SpringBoot06:自定義starter

我們分析完畢了源碼以及自動裝配的過程,我們可以嘗試自定義一個啓動器來玩玩!

說明

啓動器模塊是一個 空 jar 文件,僅提供輔助性依賴管理,這些依賴可能用於自動裝配或者其他類庫;

命名歸約:

官方命名:

  • 前綴:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web…

自定義命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter

編寫啓動器

  1. 在IDEA中新建一個空項目 spring-boot-starter-diy

  2. 新建一個普通Maven模塊:godfrey-spring-boot-starter

  3. 新建一個Springboot模塊:godfrey-spring-boot-starter-autoconfigure

  1. 點擊apply即可,基本結構

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-rjohFI6i-1590636092563)(C:\Users\leomo\AppData\Roaming\Typora\typora-user-images\image-20200528103104503.png)]

  1. 在我們的 starter 中 導入 autoconfigure 的依賴!
<!--啓動器-->
<dependencies>
    <!--引入自動配置模塊-->
    <dependency>
        <groupId>com.godfrey</groupId>
        <artifactId>godfrey-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
  1. 將 autoconfigure 項目下多餘的文件都刪掉,pom中只留下一個 starter,這是所有的啓動器基本配置!

  1. 編寫HelloProperties 配置類
package com.godfrey;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * description : 服務properties配置類
 *
 * @author godfrey
 * @since 2020-05-28
 */
//前綴 godfrey.hello
@ConfigurationProperties(prefix = "godfrey.hello")
public class HelloProperties {

    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
  1. 編寫一個自己的服務
package com.godfrey;

/**
 * description : 自定義服務
 *
 * @author godfrey
 * @since 2020-05-28
 */
public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix() + name + helloProperties.getSuffix();
    }
}
  1. 編寫我們的自動配置類並注入bean,測試!
package com.godfrey;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description : 配置類
 *
 * @author godfrey
 * @since 2020-05-28
 */
@Configuration
@ConditionalOnWebApplication //web應用生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}
  1. 在resources編寫一個自己的 META-INF\spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.godfrey.HelloServiceAutoConfiguration
  1. 編寫完成後,可以安裝到maven倉庫中!

新建項目測試我們自己寫的啓動器

  1. 新建一個SpringBoot 模塊

  1. 導入我們自己寫的啓動器
<dependency>
	<groupId>com.godfrey</groupId>
    <artifactId>godfrey-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  1. 編寫一個Controller 進行測試我們自己的寫的接口!
package com.godfrey.controller;

import com.godfrey.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description : HelloController
 *
 * @author godfrey
 * @since 2020-05-28
 */
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello() {
        return helloService.sayHello("Hello godfrey spring boot start");
    }
}
  1. 編寫配置文件 application.yml
godfrey:
  hello:
    prefix: "Printprefix "
    suffix: " Printsuffix"
  1. 啓動項目進行測試,結果成功 !

.

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