springboot制作自定義starter


自定義starter

SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統一集成進 starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息並啟 動相應的默認配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種信息的困擾。 SpringBoot會自動通過classpath路徑下的類發現需要的Bean,並注冊進IOC容器。SpringBoot提供 了針對日常企業應用研發各種場景的spring-boot-starter依賴模塊。所有這些依賴模塊都遵循着約定 成俗的默認配置,並允許我們調整這些配置,即遵循“約定大於配置”的理念。

為什么要自定義starter

在日常開發工作中,經常會有一些獨立於業務之外的配置模塊,我們經常將其放到一個特定的 包下,然后如果另一個工程需要復用這塊功能的時候,需要將代碼硬拷貝到另一個工程,重新集成一遍,比較麻煩。如果我們將這些可獨立於業務代碼之外的功配置模塊封裝成一個個starter,復用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配。

自定義starter的命名規則

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建議自定義的starter使用 xxx-spring-boot-starter命名規則。以區分SpringBoot生態提供的starter。

 

操作步驟

1.新建spring工程

這個應該不用多說,使用idea中Spring Initializr即可構建

工程結構如下

 

2.pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>demo-spring-boot-starter</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

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

</project>

 

3.定義一個實體類映射配置信息

package demospringbootstart.demo.properties;

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

/**
 * @Author:VIC
 * @Date:Created in 15:04 2020/04/09
 * @Description:配置信息實體類
 */
// 該注解可以把相同前綴的配置信息通過配置項名稱映射成實體類,這里我們指定前綴"demo",
// 就可以將以"demo"為前綴的配置項信息拿到
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {

    private String sayWhat;
    private String toWho;

    public String getSayWhat() {
        return sayWhat;
    }

    public void setSayWhat(String sayWhat) {
        this.sayWhat = sayWhat;
    }

    public String getToWho() {
        return toWho;
    }

    public void setToWho(String toWho) {
        this.toWho = toWho;
    }
}

 

4.定義配置類

package demospringbootstart.demo.config;

import demospringbootstart.demo.properties.DemoProperties;
import demospringbootstart.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @Author:VIC
 * @Date:Created in 15:35 2020/04/09
 * @Description:配置類
 */
//@Configuration聲明為一個配置類,沒什么好說的
@Configuration
//該注解用來開啟上面一步 @ConfigurationProperties 注解配置bean的支持,
//就是告訴springboot能夠支持@ConfigurationProperties,
// 或者你也可以在@ConfigurationProperties注解標識的類上添加@Configuration和@Component,效果一樣
@EnableConfigurationProperties(DemoProperties.class)
//@ConditionalOnProperty用來控制@Configuration是否生效,簡單來說也就是我們可以通過在yml或properties配置文件中控制 @Configuration 注解的配置類是否生效。
@ConditionalOnProperty(prefix = "demo", name = "isopen", havingValue = "true")
public class DemoConfig {

    @Autowired
    private DemoProperties properties;

    @Bean
    public DemoService demoService() {
        return new DemoService(properties.getSayWhat(), properties.getToWho());
    }
}

 

5.提供一個service類

package demospringbootstart.demo.service;

/**
 * @Author:VIC
 * @Date:Created in 15:28 2020/04/09
 * @Description:隨便實現一個service,這里沒什么好說的,這個service提供給集成該starter的業務方調用
 */
public class DemoService {

    public String sayWhat;
    public String toWho;

    public DemoService(String sayWhat, String toWho) {
        this.sayWhat = sayWhat;
        this.toWho = toWho;
    }

    public String sayHello() {
        return this.sayWhat + "!!!" + this.toWho;
    }
}

 

6.新建spring.factories文件(重要)

將spring.factories放到META-INF文件夾下面,spring.factories文件內容如下

#自動裝配,至於為什么配置了這個文件,springboot就會去裝配這里配置的Democonfig類,讀者可以去看看springboot SPI機制的實現(有時間搞一篇博客)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=demospringbootstart.demo.config.DemoConfig

 

7.測試使用

以上步驟完成后,然后只需mvn clean install,一個自定義starter就制作完成了

======================分隔線======================

使用自定義的starter

1.引入starter依賴

<dependency>
    <groupId>demo-spring-boot-starter</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2.填寫配置文件內容

demo.isopen=true
demo.to-who=JACK
demo.say-what=HI

3.寫段代碼調用一下看看

package com.zhaowa.course.design.controller;

import demospringbootstart.demo.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @Author:VIC
 * @Date:Created in 15:53 2020/04/09
 * @Description:
 */
@RestController
public class TestController {

    @Resource
    private DemoService demoService;

    @RequestMapping("/")
    public String test(){
        return demoService.sayHello();
    }
}


免責聲明!

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



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