squirrel-spring-boot-starter

一、Spring Boot Starter简介

Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件)进行自动配置。使用者只需要依赖相应功能的Starter,无需做过多的配置和依赖,Spring Boot就能自动扫描并加载相应的模块。SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

二、Starter的开发步骤

  1. 新建Maven项目,在项目的POM文件中定义使用的依赖;
  2. 新建配置类,写好配置项和默认的配置值,指明配置项前缀;
  3. 新建自动装配类,使用@Configuration和@Bean来进行自动装配;
  4. 新建spring.factories文件,指定Starter的自动装配类;

三、squirrel-spring-boot-starter实现过程

1、maven添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-configuration-processorartifactId>
        <optional>trueoptional>
    dependency>
    <dependency>
        <groupId>org.squirrelframeworkgroupId>
        <artifactId>squirrel-foundationartifactId>
        <version>0.3.8version>
    dependency>
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
    dependency>
dependencies>

2、新建配置类,写好配置项和默认的配置值,指明配置项前缀。

@Data
@ConfigurationProperties(prefix = "state.machine")
public class StateMachineProperties {

    /**
     * 状态机类的全限定名
     */
    private String clazz;


    /**
     * 是否启动默认配置
     */
    private boolean autoConfig;

}

3、新建自动装配类,使用@Configuration和@Bean来进行自动装配;
,创建builder并注入。

@Configuration
@EnableConfigurationProperties(StateMachineProperties.class)
public class StateMachineConfig {

    @Resource
    private StateMachineProperties stateMachineProperties;

    @Bean
    public UntypedStateMachineBuilder builder() throws ClassNotFoundException {
       if (stateMachineProperties.isAutoConfig()) {
           Class clazz =  Class.forName(stateMachineProperties.getClazz());
           if (clazz == null) {
               throw new SquirrelAutoConfigException("指定状态机类不存在");
           }
           UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(clazz);
           return builder;
       }
       return null;
    }
}

4、定义配置异常类

public class SquirrelAutoConfigException extends RuntimeException {

    private static final long serialVersionUID = -4606172299202646819L;


    public SquirrelAutoConfigException() {
        super();
    }

    public SquirrelAutoConfigException(String message) {
        super(message);
    }

    public SquirrelAutoConfigException(String message, Throwable cause) {
        super(message, cause);
    }

    public SquirrelAutoConfigException(Throwable cause) {
        super(cause);
    }
}

5、创建状态机后处理类,在创建状态机后注入到Spring中。

@Component
public class StateMachineAutowireProcessor implements SquirrelPostProcessor<StateMachine>, ApplicationContextAware {
    private ApplicationContext applicationContext;

    public StateMachineAutowireProcessor() {
        // 注册后处理器
        SquirrelPostProcessorProvider.getInstance().register(StateMachine.class, this);
    }

    @Override
    public void postProcess(StateMachine stateMachine) {
        Preconditions.checkNotNull(stateMachine);
        // 将状态机注入到Spring
        AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
        autowireCapableBeanFactory.autowireBean(stateMachine);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

6、新建spring.factories文件,指定Starter的自动装配类,spring.factories文件位于resources/META-INF目录下,需要手动创建;
org.springframework.boot.autoconfigure.EnableAutoConfiguration后面的类名说明了自动装配类,如果有多个 ,则用逗号分开;

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.squirrelframework.foun

7、application.yml增加默认配置

state:
  machine:
    auto-config: false

你可能感兴趣的:(Squirrel,State,Machine)