迟早要还系列,实现一个 Spring Boot Starter

2020-07-11  本文已影响0人  大继

前言

实现一个Spring boot 的starter 也是学习spring boot 的一个关键步骤,由于一直没有学习,所以一直没有使用过starter 这是个恶性循坏。

须知

未了简单跟好了解下面的知识,最好是有了解过Spring boot的启动过程。
我这里就简单说一些跟编写starter 相关的知识点:
META-INF/spring.factories 里面就是配置starter 自定义处理增强或补充,

Starter 的定义

通常一个完整的 starter 需要包含下面两个组件:

1.Auto-Configure Module
一定的配置能力

2.Starter Module
提供 "启动" 某个特性所需的所有依赖项。可以包含一个或多个 Auto-Configure Module (自动配置模块)的依赖项,以及可能需要的任何其他依赖项。

什么时候需要starter

一个通用的模块,块的大小要能完整的实现一个以上的功能,以面向对象的思维来描述:
xxx-starter-轮胎、里面包含了很多轮毂,轮胎,的制作工具及一些可以直接使用标准的轮胎。

开始

1.AutoConfigurationClass
@Configuration
@EnableConfigurationProperties(AutoConfigurationProperties.class)
@ConditionalOnClass(GetHashCodeClass.class)
public class AutoConfigurationClass {


    @Autowired
    private AutoConfigurationProperties autoConfigurationProperties;


    /**
     * @ConditionalOnMissingBean 当spring容器中没有这个Bean的时候才进行下一步
     * @return
     */
    @ConditionalOnMissingBean
    @Bean
    public GetHashCodeClass getHashCodeClass(){
        return new GetHashCodeClass(autoConfigurationProperties.getTarget());
    }
}
2.AutoConfigurationProperties
@ConfigurationProperties("van.demo")
public class AutoConfigurationProperties {

    private String target = "default";

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }
}

3.DemoFailureAnalyzer
public class DemoFailureAnalyzer extends AbstractFailureAnalyzer<DemoFailureAnalyzerException> {


    /**
     * SpringBoot在启动时会创建bean时的异常描述转义
     * @param failure
     * @param cause
     * @return
     */
    @Override
    public FailureAnalysis analyze(Throwable failure,DemoFailureAnalyzerException cause) {

//        failure.printStackTrace();
        System.out.println("我捕获了自己的异常.");

        return new FailureAnalysis("DemoFailureAnalyzer 测试", "这里是解决的方法描述", cause);
     }
}

4.DemoFailureAnalyzerException
public class DemoFailureAnalyzerException extends RuntimeException{
    public DemoFailureAnalyzerException(String msg){
        super(msg);
    }
    public DemoFailureAnalyzerException(String msg, Throwable t) {
        super(msg, t);
    }
}

5.GetHashCodeClass
public class GetHashCodeClass {

    private String target;
    public GetHashCodeClass(String target){
        this.target = target;
    }

    public String getHashCode(){
        return String.valueOf(this.target.hashCode());
    }
}

6. resources/META-INFO/spring.factories
# 启动时吧resource文件加载到该配置进行特殊处理
org.springframework.boot.env.PropertySourceLoader=\

# 启动时给各个监听进行通知的通知器
org.springframework.boot.SpringApplicationRunListener=\

# 对应用的事件进行监听
org.springframework.context.ApplicationListener=\
  
# 类初始化异常处理,正常我们用不上spring boot 用就好
org.springframework.boot.SpringBootExceptionReporter=\

# 应用上下问初始化
org.springframework.context.ApplicationContextInitializer=\

# 自定义的配置信息
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.fobe.starter.demo.AutoConfigurationClass

# 自定义启动时配置项,可以在应用启动前做对springApplication 进行一些配置操作。
org.springframework.boot.env.EnvironmentPostProcessor=\

# 自定义的类初始化,失败错误信息描述,这里更偏向 解决方法的描述
org.springframework.boot.diagnostics.FailureAnalyzer=\
cn.fobe.starter.demo.DemoFailureAnalyzer

测试

1.添加依赖
<dependency>
            <groupId>cn.fobe</groupId>
            <artifactId>van-starter-demo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
2.application.yml
van:
    demo:
        target: aaa
spring:
    datasource:
        url: jdbc:h2:mem:demo_db
        username: root
        password: root
        driverClassName: org.h2.Driver

3.main
public static void main(String[] args) {

 
        ConfigurableApplicationContext context =  SpringApplication.run(SpringDemoApplication.class,args);

        GetHashCodeClass getHashCodeClass = (GetHashCodeClass) context.getBeanFactory().getBean("getHashCodeClass");

        System.out.println(getHashCodeClass.getHashCode());
    }

4.输出

aaa

疑惑

1.为何spring-boot-starter-data-jpa 里面没有spring.factories文件

因为依赖的spring-boot-data-jpa 包含了spring.factories


image.png
2.如果需要找到@Configuration的类进行加载JVM启动时并不加载.class文件信息那么怎么知道那个类有@Configuration 难道全局加载,想着就不可能这么傻.

跟了一下代码没找着,有知道的同学麻烦来一下,毕竟干啃很难受。又不想花太多时间。

参考

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/howto.html#howto

https://www.cnblogs.com/FraserYu/p/11833604.html

上一篇 下一篇

猜你喜欢

热点阅读