前言:
想要自定义starter,首先要了解springboot是如何加载starter的,也就是springboot的自动装配机制
starter加载原理:
springboot通过一个@SpringBootApplication注解启动项目,springboot在项目启动的时候,会将项目中所有声明为Bean对象(注解、xml)的实例信息全部加载到ioc容器当中。 除此之外也会将所有依赖到的starter里的bean信息加载到ioc容器中,从而做到所谓的零配置,开箱即用。
加载starter:
 通过@EnableAutoConfiguration注解进行加载starter,@EnableAutoConfiguration在@SpringBootApplication注解里面
 
 具体的加载实现是由@EnableAutoConfiguration注解下import了一个AutoConfigurationImportSelector加载器实现
 
 这个AutoConfigurationImportSelector会去所引用的依赖jar包下,找到一个”spring.factories”文件,一般spring.factories文件里都会声明该依赖所提供的核心功能bean配置信息。文件一般在依赖jar包的META-INF文件夹下面
 
找到后将这个文件里声明的配置信息进行加载,从而达到加载全部bean信息的效用。
自定义starter:
上面了解了springboot加载starter原理,其实就是加载依赖jar包下的spring.factories文件。所以我们要自定义starter,就需要在项目中建立一个META-INF的文件夹,然后在文件夹下面建一个spring.factories文件,文件里将你需要提供出去的bean实例信息配置好就行。
代码
新建springboot项目。简单演示所以需求配置任务依赖。如springboot构建很慢,或者打包的时候下载依赖很慢,可在pom文件中添加如下配置。可以加快构建速度
 <repositories>
        <repository>
            <id>alimaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>alimaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </pluginRepository>
    </pluginRepositories>
项目构建完成后,在resources文件夹下面新建META-INF文件夹,并新建spring.factories文件
 
 因为我们是作为提供服务模块,所以不需要启动类,删除启动类并新建服务提供和统一配置两个类
 统一配置类:
package com.yxj.demostarter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DemoConfig {
    @Value("${demo}")
    private String demo;
    @Bean
    public DemoService demoService(){
        return new DemoService(demo);
    }
}
服务提供类:
package com.yxj.demostarter;
public class DemoService {
    private String demo;
    public DemoService(String demo) {
        this.demo = demo;
    }
    public String getService(){
        return demo;
    }
}
新建好后,在spring.factories文件里,将上面的统一配置类DemoConfig配置到文件里
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yxj.demostarter.DemoConfig
配置完后就可以开始maven install安装到maven仓库中了
mvn clean install -Dmaven.test.skip=true
如果安装的时候报错:Unable to find main class
 
 那删掉pom文件中的打包插件配置
 
 安装好后,就会在你的本地maven仓库出现一个jar包,之后其他的项目就可以从这个本地maven仓库里获取依赖
依赖测试:
在另一个项目中,引入刚刚打包的pom依赖
 <dependency>
            <groupId>com.yxj</groupId>
            <artifactId>demoStarter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
而后新建一个测试controller,里面注入上面提供的DemoService类。
 然后调用其方法。
package com.yxj.yosmfa.controller;
import com.yxj.demostarter.DemoService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
    private DemoService demoService;
    @GetMapping("demo")
    public ResponseEntity demo(){
        String service = demoService.getService();
        return new ResponseEntity(service, HttpStatus.OK);
    }
    public DemoController(DemoService demoService) {
        this.demoService = demoService;
    }
}
此时还要注意,提供服务的starter里有引用一个demo属性
 
 所以需要在你测试项目的yml文件里加一个demo属性的配置
 
 所有准备完成后,启动项目,调用测试类demo方法
 
 可以看到正常将测试项目配置文件里的demo属性,通过自定义starter提供的服务,调用返回了。且测试项目并没有为自定义starter项目做任何bean的配置,就能在测试项目的controller中注入使用DemoService类的功能。
最后
最后再来看下,我们自定义的springbootstarter在测试项目中的依赖情况
 
 这就印证了上面说的,sprinboot是通过加载META-INF文件夹下的spring.factories文件完成自动配置的功能以及开箱即用的效果
