美文网首页
springBoot的Starter

springBoot的Starter

作者: 剑道_7ffc | 来源:发表于2020-04-26 10:48 被阅读0次

什么是 Starter

Starter是开箱即用的模块,因为starter内部根据自动装配实现了bean的注入,所以只需要引用该依赖jar就可以了,不需要额外的配置和依赖。

手写starter

需求

将对象转换为对应的字符串

源码

新建一个maven项目,依赖包如下

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.2.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <version>2.2.2.RELEASE</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.58</version>
      <optional>true</optional>
    </dependency>
  </dependencies>

format包如下

public interface Format {
    /**
     * 转换
     */
    <T> String format(T obj);
}
public class StringFormat implements Format{

    @Override
    public <T> String format(T obj) {
        return String.valueOf(obj);
    }
}
public class FastJsonFormat implements Format {
    @Override
    public <T> String format(T obj) {
        return JSON.toJSONString(obj);
    }
}

config包如下

@Configuration
public class FormatConfig {

    @Bean
    @Primary
    @ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
    public Format createStringFormat(){
        return new StringFormat();
    }

    @Bean
    @ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
    public Format createFastJsonFormat(){
        return new FastJsonFormat();
    }
}
@ConfigurationProperties(prefix = FormatProperties.prefix)
public class FormatProperties {
    public static final String prefix = "edward.format";

    private Map<String,Object> info;

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }
}
@Import(value = FormatConfig.class)
@EnableConfigurationProperties(FormatProperties.class)
@Configuration
public class FormatTemplateConfig {

    @Bean
    public FormatTemplate createFormatTemplate(FormatProperties formatProperties, Format format){
        return new FormatTemplate(formatProperties, format);
    }

}
public class FormatTemplate {
    private FormatProperties formatProperties;
    private Format format;

    public FormatTemplate(FormatProperties formatProperties, Format format) {
        this.formatProperties = formatProperties;
        this.format = format;
    }

    public <T> String doFormat(T obj){
        System.out.println("对象:" + format.getClass());
        System.out.println("参数:" + format.format(formatProperties.getInfo()));

        return format.format(obj);
    }
}

spring.factories如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.edward.config.FormatTemplateConfig
运行

application.properties参数

edward.format.info.name=edward
edward.format.info.age=18
@RestController
public class FormatController {
    @Autowired
    private FormatTemplate formatTemplate;

    @GetMapping(value = "/format")
    public String format(){
        User user = new User();
        user.setName("edward");
        user.setAge(18);

        return formatTemplate.doFormat(user);
    }
}
image.png

spring-boot-starter-logging

简介

接口和实现:JCL(Apache Commons Logging)-->Log4j、Log4j2
接口和实现:Slf4j-->Logback

相关文章

网友评论

      本文标题:springBoot的Starter

      本文链接:https://www.haomeiwen.com/subject/swbrwhtx.html