吊炸天-自定義starter

對於自動裝配的原理進行分析之後,我們可以基於這個機制來實現一個Starter組件,以便加深大家對自動裝配以及Starter組件的理解。而且官方提供的Starter並不能包含所有的技術組件,所有工作中我們也有可能會自己開發組件

簡單介紹下Starter組件的主要三個功能

1.涉及相關組件的jar包依賴

2.自動實現Bean的裝配

3.自動聲明並且加載application.properties

 

正式開始前先給大家普及下starter的命名規範

1.官方格式:spring-boot-starter-模塊名稱 例如:spring-boot-starter-web

2.自定義: 模塊名稱-spring-boot-starter 例如:mybatis-spring-boot-starter

 

開始 go

工程目錄

 

pom依賴

<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

定義實體類映射配置信息

@ConfigurationProperties(prefix = "") 要和@Component配合使用,否則有坑,具體不在這說明了
package com.mystarter.conf;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ClassName MyProperties
 * @Description TODO
 * @Author moran
 * @Date 2020/6/14 11:28
 **/
@ConfigurationProperties(prefix = "my")
@Component
@Data
public class MyProperties {

    private String name;
    private String age;
}

定義APiService

package com.mystarter.service;

import com.mystarter.conf.MyProperties;

/**
 * @ClassName HelloService
 * @Description TODO
 * @Author moran
 * @Date 2020/6/14 11:52
 **/
public class HelloService {

    private MyProperties myProperties;

    public MyProperties getMyProperties(){
        return myProperties;
    }

    public void setMyProperties(MyProperties myProperties){
        this.myProperties = myProperties;
    }

    public String sayHello(){
        return myProperties.getName()+"--"+myProperties.getAge()+"hello";
    }
}

核心配置類

package com.mystarter.conf;

import com.mystarter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RedissonAutoConfiguration
 * @Description TODO
 * @Author moran
 * @Date 2020/6/14 11:39
 **/
@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class MyAutoConfiguration {

    @Autowired
    private MyProperties myProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setMyProperties(myProperties);
        return  helloService;
    }

}

最重要的來了  在resources 下建立 META-INF目錄 並新建 spring.factories 

具體內容如下

#-------starter自動裝配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mystarter.conf.MyAutoConfiguration

然後maven instal 就完事了,將生成的jar 引入maven倉庫,就可以在其他項目中使用了

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