自定义Starter

01.引入依赖



    org.springframework.boot
    spring-boot-autoconfigure
    2.7.2

02.创建标记(只有引用此starter的应用中扫描到标记类才开启自动配置)

public class WorkflowConfigMarker {
}

03.配置开关(添加配置开关注解,才会扫描到标记类才会开启自动配置)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(WorkflowConfigMarker.class)
public @interface EnableWorkflowRegisterServer {
}

04.配置容器(只有扫描到标记类时配置容器才会生效 ,才会注入bean)

@Configuration
@ConditionalOnBean(WorkflowConfigMarker.class)
public class WorkflowAutoConfiguration {

    static {
        System.out.println("WorkflowAutoConfiguration init ...");
    }

    @Bean
    public WorkflowProperties simpleBean(){
        return new WorkflowProperties();
    }
}

05.配置参数(配置参数可以在引用此starter项目的配置文件中配置)

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "workflow")
public class WorkflowProperties {
    
    private String configurationParameter1;

    private String configurationParameter2;

    public String getConfigurationParameter1() {
        return configurationParameter1;
    }

    public void setConfigurationParameter1(String configurationParameter1) {
        this.configurationParameter1 = configurationParameter1;
    }

    public String getConfigurationParameter2() {
        return configurationParameter2;
    }

    public void setConfigurationParameter2(String configurationParameter2) {
        this.configurationParameter2 = configurationParameter2;
    }
}

06.创建文件(创建 :/ resource/ META-INF/ spring.factories 文件)

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lagou.configuration.WorkflowAutoConfiguration

你可能感兴趣的:(自定义Starter)