SpringBoot 项目就是由一个一个 Starter 组成的,一个 Starter 代表该项目的 SpringBoot 起步依赖,除了官方已有的 Starter,如果你需要将自己的项目支持 SpringBoot,那么就需要把它制作成一个 Starter。这篇文章小编将依据 SpringBoot 的自动化配置原理,开发一个属于自己的 Starter。

自定义自己的 Starter

  所谓的 Starter ,其实就是一个普通的 Maven 项目,因此我们自定义 Starter ,需要首先创建一个普通的 Maven 项目,创建完成后,添加 Starter 的自动化配置类即可,如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>

  配置完成后,我们首先创建一个 HelloProperties 类,用来接受 application.properties 中注入的值,如下:

@ConfigurationProperties(prefix = "yexiaomo")
public class HelloProperties {
    private static final String NAME = "zmf";
    private static final String MSG = "知性人";
    private String name = NAME;
    private String msg = MSG;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

  配置完成 HelloProperties 后,接下来我们来定义一个 HelloService ,然后定义一个简单的 sayHello方法, HelloService 的定义如下:

public class HelloService {
    private String name;
    private String msg;

    public String sayHello() {
        return name + ",say :" + msg;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

  接下来我们就自定义自己的自动配置类:

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {

	//自动配置类中首先注入 HelloProperties ,这个实例中含有我们在 application.properties 中配置的相关数据。
    @Autowired
    HelloProperties helloProperties;
    
    @Bean
    HelloService helloService() {
    //提供一个 HelloService 的实例,将 HelloProperties 中的值注入进去。
        HelloService helloService = new HelloService();
        helloService.setName(helloProperties.getName());
        helloService.setMsg(helloProperties.getMsg());
        return helloService;
    }
}
  • @Configuration: 注解表明这是一个配置类。
  • @EnableConfigurationProperties: 注解是使我们之前配置的 @ConfigurationProperties 生效,让配置的属性成功的进入 Bean 中。
  • @ConditionalOnClass: 表示当项目当前 classpath 下存在 HelloService 时,后面的配置才生效。

  然后我们在 Maven 项目的 resources 目录下创建一个名为 META-INF 的文件夹,然后在文件夹中创建一个名为 spring.factories 的文件,文件内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.yexiaomo.HelloServiceAutoConfiguration

注意:这个属性文件可重要啦,因为 SpringBoot 自动化配置最终就是要扫描 META-INF/spring.factories 来加载项目的自动化配置类。

把自定义的 Starter 安装到本地仓库

  我将这个自动化配置类安装到本地仓库,然后方便其他项目中使用即可。安装方式很简单,在 IntelliJ IDEA 中,点击右边的 Maven Project ,然后选择 Lifecycle 中的 install ,双击即可,如下:

springboot start自定义 自定义一个springboot starter_自定义一个 starter

使用 Starter

  首先我们新建一个普通的 Spring Boot 工程,这个 Spring Boot 创建成功之后,加入我们自定义 Starter 的依赖,如下:

<dependency>
    <groupId>org.yexiaomo</groupId>
    <artifactId>mystarter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

创建一个 Controller 测试

代码如下:

@Controller
public class HelloController {

    @Autowired
    HelloService helloService;

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

启动项目访问:http://localhost:8080/hello

springboot start自定义 自定义一个springboot starter_自定义一个 starter_02


如果我们在 application.properties 中配置如下:

yexiaomo.name=知性人
yexiaomo.msg=yexiaomo.javaboy.org

重新启动项目访问:http://localhost:8080/hello

springboot start自定义 自定义一个springboot starter_spring_03


如果中文出现乱码,请修改如下配置,然后从新定义 application.properties 中的内容即可:

springboot start自定义 自定义一个springboot starter_自定义_04


到此结束,一个简单的自动化配置类我们就算完成了,谢谢阅览!!!