avatar

SpringBoot自定义starters

SpringBoot中的starter

在SpringBoot中已经实现的场景启动器需要注意:
1、这个场景需要使用到的依赖是什么
2、如何编写自动配置

  • 自动装配Bean: 自动装配使用配置类(@Configuration)结合Spring4 提供的条件判断注解@Conditional及Spring Boot的派生注解如@ConditionOnClass完成;
  • 配置自动装配Bean: 将标注@Configuration的自动配置类,放在classpath下META-INF/spring.factories文件中.
  • 自动装配顺序:
    • 在特定自动装配Class之前:@AutoConfigureBefore
    • 在特定自动装配Class之后:@AutoConfigureAfter
    • 指定顺序:@AutoConfigureOrder
@Configuration  //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件

@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中

自动配置类要能加载
将需要启动就加载的自动配置类,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

3、注意一些SpringBoot编写场景启动器的模式

  • 启动器只用来做依赖导入:启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库
  • 专门来写一个自动配置模块;
  • 启动器依赖自动配置;别人只需要引入启动器(starter)
  • 命名规约
    • 官方命名空间
      1、前缀:“spring-boot-starter-”
      2、模式:spring-boot-starter-模块名
      3、举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
    • 自定义命名空间
      1、后缀:“-spring-boot-starter”
      2、模式:模块-spring-boot-starter
      3、举例:mybatis-spring-boot-starter

编写自己的场景启动器

首先我们创建两个项目,一个项目是场景启动器,另一个是场景启动器的配置类
1、场景启动器是用来添加到相应的包中使用,作为一个启动器,是一个maven工程,因为场景启动器是一个空的,只是为了提供引入的依赖
2、启动器配置类是一个springboot工程,并且是添加到自定义场景启动器中。

  • 创建一个空的项目

    • 在空项目中创建一个maven项目,这个maven项目就是我们的场景启动器:zenshin-spring-boot-starter
    • 同样在空项目中创建一个SpringBoot项目,这个项目是我们的自动配置类: zenshin-spring-boot-starter-autoconfigurer
  • 将自动配置类放入场景启动器:
    在zenshin-spring-boot-starter项目中的pom添加如下代码

    <!--启动器-->
    <dependencies>
    <!--引入自动配置模块-->
    <dependency>
    <groupId>com.zenshin.starter</groupId>
    <artifactId>zenshin-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    </dependencies>

    这样就将自动配置zenshin-spring-boot-starter-autoconfigurer项目引入到了启动器中

  • 编写自动配置项目

    • 创建一个properties,用来获取配置
      @ConfigurationProperties(prefix = "zenshin.hello")
      public class HelloProperties {
      public String getPrefix() {
      return prefix;
      }

      public void setPrefix(String prefix) {
      this.prefix = prefix;
      }

      public String getSuffix() {
      return suffix;
      }

      public void setSuffix(String suffix) {
      this.suffix = suffix;
      }

      private String prefix;
      private String suffix;

      }
    • 我们需要编写一个bean去使用配置,作为对外的输出,也就是真正对外起作用的类
      public class HelloService {

      HelloProperties helloProperties;

      public HelloProperties getHelloProperties() {
      return helloProperties;
      }

      public void setHelloProperties(HelloProperties helloProperties) {
      this.helloProperties = helloProperties;
      }

      public String sayHello(String name)//这就是外界要使用的方法
      {
      return helloProperties.getPrefix()+"-"+name+"-"+helloProperties.getSuffix();
      }
      }
    • 这两者都准备好以后我们需要编写自动配置类,也就是最核心的类
      @Configuration
      @ConditionalOnWebApplication//web应用才会生效
      @EnableConfigurationProperties(HelloProperties.class)//将配置类导入,将配置类与配置挂钩,并且将配置类注册到IOC容器中
      public class HelloServiceAutoConfiguration {
      @Autowired
      HelloProperties helloProperties;//从容器中拿出配置类

      @Bean
      public HelloService helloService()
      {
      HelloService service = new HelloService();
      service.setHelloProperties(helloProperties);
      return service;
      }

      }
    • 还有最重要的一步:就是将自动配置类放到MATE-INF文件夹下,让SpringBoot进行识别,在资源文件下创建一个MATE_INF文件夹,创建spring.factories文件,然后写入下面的代码
      # Auto Configure
      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      com.zenshin.starter.HelloServiceAutoConfiguration
    • 自动配置项目就完成了
  • 将两个项目加到Maven仓库中

    • 先将自动配置项目加入,再加入启动器项目,这是有先后顺序的。
    • 加入Maven就是在各自的项目中
  • 创建一个SpringBoot项目,使用我们的自定义启动器

    • 在pom文件中添加
      <!--引入自定义的starter-->
      <dependency>
      <groupId>com.zenshin.starter</groupId>
      <artifactId>zenshin-spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
      </dependency>
    • 编写配置文件
      zenshin.hello.prefix = zenshin
      zenshin.hello.suffix = Hello World
    • 写一个简单的controller测试一下
      @RestController
      public class helloController {

      @Autowired
      HelloService helloService;

      @GetMapping("/hello")
      public String Hello()
      {
      return helloService.sayHello("zenshin");
      }
      }
    • 如果输入网址能在浏览器中输出想要的结果那么自定义的启动器就成功了。
文章作者: zenshin
文章链接: https://zlh.giserhub.com/2020/04/23/springboot/starters/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 zenshin's blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论