A preface

We mentioned earlier that the definition of autorun is to try to assemble jar dependencies added by the developer under the Class Path, not necessarily. This is the theory, and SpringBoot starter is his implementation, the theory needs to land, it is necessary to write a.

The second practice

  1. Load the core configuration class

    / * * * @ author yu * @ 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); }}Copy the code

2. @ Import (FormatAutoConfiguration. Class) loading FormatAutoConfiguration. Class

@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(); }}Copy the code

Note that @Primary is the default implementation chosen when there are multiple implementations

  1. Access to the configuration

    ** * @description ConfigurationProperties: @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; }}Copy the code
  2. Creating an Abstract Class

    @date 2021/8/3 */ public interface FormatProcessor {/** * define a format method ** @param obj * @param <T> * @return */ <T> String format(T obj); }Copy the code
  3. Create a concrete implementation

    /** * @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); }} ` ` `Copy the code
  4. Encapsulating the calling object

    @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); }}Copy the code
  5. Write spring.factories and put them in the meta-INF directory

    org.springframework.boot.autoconfigure.EnableAutoConfiguration = service.conf.QianYunFormatConfiguration
    Copy the code
  6. The MVN install package completes the writing of a springbootstarter

  7. call

    1) Introducing Maven dependencies

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

    2) Add configuration to the configuration file to determine which bean to select

         ```
         qianyu.format.type=gson
         ```
     
    Copy the code
    1. Call method
      /** * @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. Elegantly-named setName (" zi yu "); String format = forTemplate.format(user); System.out.println(format); }}Copy the code

    GsonFormatProcesse{“name”:” subfeather “,”age”:18}, call successful