SpringBoot—自定义Starter篇

x33g5p2x  于2022-02-14 转载在 Spring  
字(3.9k)|赞(0)|评价(0)|浏览(316)

一、自定义Starter 的思路:

  1. 创建一个Maven工程,创建三个模块
  2. 一个模块为demo-app,一个模块为demo-module,一个模块为demo-module-springboot-starter
  3. demo-module中定义一个MyModule类,其中有一个save方法,两个属性:version,age
  4. demo-module-springboot-starter中定义一个自动配置类ModuleAutoConfiguration,关联一个配置类ModuleConfig,这个配置类通过读取properties配置文件中的属性值(通过配置文件注入值)完成对象的初始化
  5. app模块中引入demo-module-springboot-starter模块,不需要初始化Module,只需要在配置文件(application.properties)中配置属性值,即可初始化Moudle,调用save方法

二、具体实现步骤

通过以下步骤实现自定义starter

1、创建一个Maven工程,创建三个模块

2、环境准备
①、他们都是基于SpringBoot的模块需要在父模块的pom文件中引入如下:

<!--springboot版本2.5.0-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--web模块mvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <packaging>pom</packaging>

②、demo-module-springboot-starter还需要额外引入demo-module模块和配置文件注入的2个依赖

<!--避免属性文件的一个提示-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

     <!--导入demo-module-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-module</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

③、demo-app是我们测试自定义的starter的所以引入我们定义的starter模块即可

<dependency>
            <groupId>org.example</groupId>
            <artifactId>demo-module-springboot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

3、 demo-module中定义一个MyModule类

package com.sqx.module;

public class MyModule {

    private String version ;

    private Integer age ;

    public void save(){
        System.out.println("my module save ... version:" + version + ",age:" + age);
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

此时我们的deomo-moudle模块的任务就完成了!

4、demo-module-springboot-starter中定义一个自动配置类ModuleAutoConfiguration

@Configuration
@EnableConfigurationProperties(ModuleConfig.class)
public class ModuleAutoConfiguration {

    @Bean
    @ConditionalOnProperty(name = {"com.sqx.version" ,"com.sqx.age"})
    public MyModule myModule(ModuleConfig moduleConfig){
        MyModule myModule = new MyModule();
        myModule.setVersion(moduleConfig.getVersion());
        myModule.setAge(moduleConfig.getAge());
        return myModule ;
    }
}

这个自动配置类会加载我们的ModuleConfig ,然后将其作为参数传入初始化MyMoudle的方法中

5、创建一个配置类ModuleConfig

@ConfigurationProperties(prefix = "com.sqx")
public class ModuleConfig {

    private String version ;

    private  Integer age ;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

我们这个配置类会将applicaiton.properties中由com.sqx开头的配置值注入到我们的这个配置类中,此时我们的第四步中,就可以获取到我们的值,然后通过@Conditional注解的判断将自动配置类初始化到我们的Spring容器当中

6、创建一个配置文件Spring.factories,并进行如下配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.sqx.bootstarter.ModuleAutoConfiguration

这个配置文件的作用是在我们的SpringBoot项目启动的时候,会扫描这个配置文件,将这个配置文件中的配置类,加载到我们的Spring容器当中,其配置内容就是告诉我们的SpringBoot我们的自动配置类的位置!

7、接下来,就是demo-app的编写,创建一个启动类DemoApp,和一个配置文件application.properties

  • 启动类DemoApp
@SpringBootApplication
public class DemoApp {
    public static void main(String[] args) {
        SpringApplication.run(DemoApp.class,args) ;
    }
}
  • application.properties
com.sqx.version=1.0
com.sqx.age=21

8、此时我们的所有步骤都完成了,我们对整个流程做一个梳理:

1、SpringBoot启动的时候,在@EnableAutoConfiguration的作用下,会去加载META-INF下的spring.factories配置文件
2、扫描上述配置文件,将其中的自动配置类注入到Spring容器当中,并且会去加载自动配置类绑定的配置类,这个配置类会去applicaiton.properties中去找到对应的值并且注入
3、通过@Condition判断自动配置类是否生效,如果生效,会将自动配置类中的@Bean加载到我们的Spring容器当中
4、此时我们在demo-app中就获取到MyModule,然后通过自动装配@Autowired获取到,接着调用其中的save方法

9、启动demo-app,进行测试

以上就是自定义Spring的一个Starter的具体流程 !

相关文章

微信公众号

最新文章

更多