springboot自定义starter

134 阅读1分钟

对于自动装配的原理进行分析之后,我们可以基于这个机制来实现一个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仓库,就可以在其他项目中使用了