狂神說SpringBoot06:自定義starter


 狂神說SpringBoot系列連載課程,通俗易懂,基於SpringBoot2.2.5版本,歡迎各位狂粉轉發關注學習。

微信公眾號:狂神說(首發)    Bilibili:狂神說Java(視頻)

未經作者授權,禁止轉載

 自定義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模塊:kuang-spring-boot-starter

 

 

 3、新建一個Springboot模塊:kuang-spring-boot-starter-autoconfigure

 

 

 4、點擊apply即可,基本結構

 

 

 5、在我們的 starter 中 導入  autoconfigure 的依賴!

<!-- 啟動器 -->
<dependencies>
    <!--  引入自動配置模塊 -->
    <dependency>
        <groupId>com.kuang</groupId>
        <artifactId>kuang-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

6、將 autoconfigure 項目下多余的文件都刪掉,Pom中只留下一個 starter,這是所有的啟動器基本配置!

 

 

 7、我們編寫一個自己的服務

package com.kuang;

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();
    }

}

8、編寫HelloProperties 配置類

package com.kuang;

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

// 前綴 kuang.hello
@ConfigurationProperties(prefix = "kuang.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;
    }
}

9、編寫我們的自動配置類並注入bean,測試!

package com.kuang;

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;

@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;
    }

}

10、在resources編寫一個自己的 META-INF\spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kuang.HelloServiceAutoConfiguration

 

11、編寫完成后,可以安裝到maven倉庫中!

 

 

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

1、新建一個SpringBoot 項目

2、導入我們自己寫的啟動器

<dependency>
    <groupId>com.kuang</groupId>
    <artifactId>kuang-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

3、編寫一個 HelloController  進行測試我們自己的寫的接口!

package com.kuang.controller;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello("zxc");
    }

}

4、編寫配置文件 application.properties

kuang.hello.prefix="ppp"
kuang.hello.suffix="sss"

 

5、啟動項目進行測試,結果成功 !

 

 小狂神溫馨提示:學完的東西一定要多下去實踐!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM