开发一个Spring Boot Starter!

  • 2019 年 10 月 3 日
  • 筆記

在上一篇文章中,我们已经了解了一个starter实现自动配置的基本流程,在这一小结我们将复现上一过程,实现一个自定义的starter。

先来分析starter的需求:

  • 在项目中添加自定义的starter依赖,自动在Spring中加载starter中的Bean;
  • 从application.properties中加载指定配置

创建项目

  1. 先创建一个名为starter的项目。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <modelVersion>4.0.0</modelVersion>        <groupId>top.ninwoo</groupId>      <artifactId>demo-starter</artifactId>      <version>1.0.0</version>      <packaging>jar</packaging>        <dependencies>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot</artifactId>              <version>2.1.6.RELEASE</version>          </dependency>      </dependencies>  </project>
  1. 在resources中创建一个META-INF的目录,并在目录中创建一个spring.factories。在这个配置中,我们只设置一个EnableAutoConfiguration项,并且对应只设置一个DemoAutoConfig配置类。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=top.ninwoo.config.DemoAutoConfig  
  1. 创建DemoAutoConfig配置类

    package top.ninwoo.config;    import org.springframework.boot.context.properties.EnableConfigurationProperties;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.Configuration;    @Configuration  @EnableConfigurationProperties(DemoStarterProperties.class)  public class DemoAutoConfig {        @Bean      DemoBean demoBean() {          return new DemoBean();      }  }

    这个配置类,我们主要使用了@Configuration和@EnableConfigurationProperties两个注解。@EnableConfigurationProperties启用一个ConfigurationProperties。

  2. 创建ConfigurationProperties对应的DemoStarterProperties

    package top.ninwoo.config;    import org.springframework.boot.context.properties.ConfigurationProperties;    @ConfigurationProperties(prefix = "top.ninwoo.demo")  public class DemoStarterProperties {        private String name = "default";      private int age = 0;        public String getName() {          return name;      }        public void setName(String name) {          this.name = name;      }        public int getAge() {          return age;      }        public void setAge(int age) {          this.age = age;      }  }

    创建一个ConfigurationProperties类。这个类主要用来从application.properties中读取配置项,并自动设置到相对应的字段上。

  3. 创建一个测试用Bean,并使用ConfigurationProperties类中的信息。

    起初这里有个疑惑,不知道如何使用这个ConfigurationProperties类。不过在spring中最常见的就是Bean,我们可以大胆的猜测通过@ConfigurationProperties注释的类,将自动在Spring容器中自动创建一个Bean。而我们在使用的时候,就通过普通的bean注入方式便可以使用ConfigurationProperties类中的信息。所以,我们这样创建一个测试Bean

    package top.ninwoo;    import javax.annotation.Resource;    public class DemoBean {      @Resource      DemoStarterProperties properties;        public String getName() {          return properties.getName();      }        public String getAge() {          return getAge();      }  }  

    同时在DemoAutoConfig中使用@Bean注解创建一个Bean。

到这里,我们的starter就创建完成了。通过mvn打包,或者创建同一个父项目的不同子Module的方式,我们可以进行测试这个starter是否生效。

创建测试类

测试类使用一个spring boot web项目来完成,主要创建了一个RestController,并通过RestController获取Spring上下文中注册的bean names和starter中的测试Bean。

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <modelVersion>4.0.0</modelVersion>        <groupId>top.ninwoo</groupId>      <artifactId>springboot-demo</artifactId>      <version>1.0.0</version>        <parent>          <artifactId>spring-boot-starter-parent</artifactId>          <groupId>org.springframework.boot</groupId>          <version>2.1.6.RELEASE</version>      </parent>        <dependencies>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>          <dependency>              <groupId>top.ninwoo</groupId>              <artifactId>demo-starter</artifactId>              <version>1.0-SNAPSHOT</version>          </dependency>      </dependencies>  </project>

在pom文件中,我们添加了刚刚实现的starter。

RestController:

@RestController  public class IndexController implements ApplicationContextAware {        ApplicationContext ctx = null;        @Resource      DemoBean demoBean;        @RequestMapping("/getList")      public String[] getBeanNames() {          return ctx.getBeanDefinitionNames();      }        @RequestMapping("/getDemoBean")      public String demoBean() {          return demoBean.getName();      }      @Override      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {          ctx = applicationContext;      }  }  

SpringBoot启动类 MainApp:

package top.ninwoo;    import org.springframework.boot.SpringApplication;  import org.springframework.boot.autoconfigure.SpringBootApplication;    @SpringBootApplication  public class MainApp {        public static void main(String[] args) {          SpringApplication.run(MainApp.class, args);      }  }

我们可以看到,与正常的一个web项目相比,我们只是添加了一个依赖,而并没有修改启动类。

测试

访问127.0.0.1:8080/getList接口,我们可以看到最后的几个bean Names是:

...,"top.ninwoo.config.DemoAutoConfig","demoBean","top.ninwoo.demo-top.ninwoo.config.DemoStarterProperties"]

这证明,通过注入我们starter依赖,已经在Spring的上下文创建了starter配置类中的Bean。

在没有设置application.properties时,直接访问http://127.0.0.1:8080/getDemoBean,可以获取到测试用的Bean实例中默认的参数配置default.

添加application.properties:

top.ninwoo.demo.name=joliu

重启项目,再次访问该接口,发现测试用的Bean实例对应的属性已经安装配置类中的参数进行设置,返回了joliu。

小结

到这里,我们可以说已经了解了开发一个SpringBoot Starter最基本的流程,我们可以尝试在我们日常的项目中开发这样的starter。