自动装配实践,自己动手写一个springboot-starter

888 阅读2分钟

一 前言

之前我们提到自动装配的定义是,尝试去装配class path下开发人员添加的jar依赖,不一定装配。这个是理论,而SpringBoot starter便是他的实现,理论需要落地,那就要自己动手写一个了。

二 实践

  1. 加载核心配置类

      /**
       * @author 子羽
       * @Description srping boot starter
       * @Date 2021/8/4
       */
      @Import(FormatAutoConfiguration.class)
      @Configuration
      @EnableConfigurationProperties(FormatProperties.class)
      public class QianYunFormatConfiguration {
          @Bean
          public ForTemplate helloFormatTemplate(FormatProperties formatProperties, FormatProcessor formatProcessor){
              if (formatProperties.getType().equals("fastjson")) {
                  return new ForTemplate(new FastJsonProcesser());
              }
    
              if (formatProperties.getType().equals("gson")) {
                  return new ForTemplate(new GsonFormatProcesser());
              }
              return new ForTemplate(formatProcessor);
          }
      }
    

2.@Import(FormatAutoConfiguration.class) 加载FormatAutoConfiguration.class

    /**
     * @author 子羽
     * @Description 条件装配 判断是否装配
     * @Date 2021/8/4
     */
    @Configuration
    public class FormatAutoConfiguration {

        @ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
        @Bean
        @Primary
        public FormatProcessor fastjsonFormat(){
            return new FastJsonProcesser();
        }
        @ConditionalOnClass(name = "com.google.gson.Gson")
        @Bean
        public FormatProcessor gsonFormat(){
            return new GsonFormatProcesser();
        }
    }
  

注意 @Primary,他是在有多个实现时,选择的默认实现

  1. 获取配置

     **
      * @author 子羽
      * @Description ConfigurationProperties 作用和@Value差不多 就是多了通配符
      * @Date 2021/8/4
      */
     @ConfigurationProperties(prefix = FormatProperties.MY_PERFIX)
     public class FormatProperties {
    
         public static final String MY_PERFIX = "qianyu.format";
    
         private String type;
    
         public String getType() {
             return type;
         }
    
         public void setType(String type) {
             this.type = type;
         }
     }
    
  2. 创建抽象类

    /**
    * @author 子羽
    * @Description 格式化的方法
    * @Date 2021/8/3
    */
    public interface FormatProcessor {
    
      /**
       * 定义一个格式化的方法
       *
       * @param obj
       * @param <T>
       * @return
       */
      <T> String format(T obj);
    }
    
  3. 创建具体实现

     /**
      * @author 子羽
      * @Description FastJson
      * @Date 2021/8/3
      */
     @Component
     public class FastJsonProcesser implements FormatProcessor {
         public <T> String format(T obj) {
             return "fastJsonFormatProcess:" + JSON.toJSONString(obj);
         }
     }
     
     
     ```
     /**
      * @author 子羽
      * @Description Gson
      * @Date 2021/8/3
      */
     public class GsonFormatProcesser implements FormatProcessor {
         public <T> String format(T obj) {
             Gson gson = new Gson();
             return "GsonFormatProcesse" + gson.toJson(obj);
         }
     }
     ```
    
  4. 封装调用对象

    /**
     * @author 子羽
     * @Description 包含具体引用
     * @Date 2021/8/4
     */
    public class ForTemplate {
    
        private FormatProcessor formatProcessor;
    
        public ForTemplate(FormatProcessor formatProcessor) {
            this.formatProcessor = formatProcessor;
        }
    
        public <T> String format(T obj){
            return formatProcessor.format(obj);
        }
    }
    
  5. 编写spring.factories 放到META-INF目录下

    org.springframework.boot.autoconfigure.EnableAutoConfiguration = service.conf.QianYunFormatConfiguration
    
  6. mvn install打包 这样就完成了一个springbootstarter的编写

  7. 调用

    1) 引入maven依赖

    <dependency>
        <groupId>star</groupId>
        <artifactId>boot-star</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    

    2)配置文件增加配置,确定选择哪一个bean

         ```
         qianyu.format.type=gson
         ```
     
    

    3) 具体调用方法 ``` /** * @author 子羽 * @Description 测试srpingbootstarter * @Date 2021/8/4 */ public class BootStarTest extends BaseTest {

         @Autowired
         public ForTemplate forTemplate;
    
         @Test
         public void formatTest(){
             User user = new User();
             user.setAge(18);
             user.setName("子羽");
             String format = forTemplate.format(user);
             System.out.println(format);
         }
     }
     ```
     
    

    输出 GsonFormatProcesse{"name":"子羽","age":18},调用成功