zl程序教程

您现在的位置是:首页 >  其他

当前栏目

SprinhgBoot2----自定义启动器

2023-03-14 22:34:15 时间

自定义启动器

前言

说明:springboot 官方给我们提供了很多启动器如:elasticsearch,aop,redis…等等

但是实际开发中,可能不同公司的业务不同需要定制化一个通用的专属的启动器来满足公司内部使用,提高开发效率。

本文将介绍怎么自定义实现一个启动器的demo流程。


项目结构,模式。

空项目:mystarter(用来把 启动器和自动配置模块 项目放在一起)

里面有两个module:

1:启动器 dhy-hello-spring-boot-starter

作用:只用来做依赖导入(启动器里面依赖自动配置模块,这样外部项目直接引用启动器就可以了)

命名规范:

springboot官方的启动器: spring-boot-starter-XXX 如:spring-boot-starter-jdbc

我们自定义的启动器:XXX-spring-boot-starter 如:hello-spring-boot-starter

2:自动配置模块 dhy-hello-spring-boot-starter-autoconfigurer

作用:具体实现启动器的业务逻辑

命名规范:

XXX-spring-boot-starter-autoconfigurer

XXX最好跟启动器的XXX保持一致!


starter启动原理

starter-pom引入 autoconfigurer 包


自定义Starter步骤

首先创建一个空项目


在空项目创建两个模块,一个启动器模块,一个自动配置模块

启动器模块可以直接创建一个maven项目,自动配置模块可以创建一个spring项目


项目配置

1:配置启动器依赖(启动器配置文件里面添加对自动配置模块项目的依赖)

    <dependencies>
        <dependency>
            <groupId>com.dhy</groupId>
            <artifactId>dhy-hello-spring-boot-starter-auotconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

2:配置自动配置模块项目依赖

注意:这里我们把 dependencies 里面只留一个最基础的springboot对starter的支持就行了

插件的引用,web的依赖都去掉

然后我们去掉了web依赖需要清理下目录结构

删除掉test目录,配置文件,和springboot的启动类,这些都不需要


3.编写自动配置模块里的业务逻辑

service:

/*
* 默认不要放在容器中
* */
public class service
{
    @Autowired
    HelloProperties helloProperties;
    public String sayHello(String userName)
    {
        return helloProperties.getPrefix()+userName+helloProperties.getSuffix();
    }
}

HelloServiceAutoConfiguration:

@Configuration
//将HelloProperties放入容器中,并与对应的配置文件进行绑定
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration
{
    //容器中没有这个组件才需要放入
    @ConditionalOnMissingBean(service.class)
    //将返回值放入容器中
    @Bean
    public service Myservice()
    {
        return new service();
    }
}

HelloProperties:

//与主配置文件中对应前缀开头的属性绑定在一起
@ConfigurationProperties("dhy.hello")
public class HelloProperties
{
    private String prefix;
    private  String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

4. 创建XXXAutoConfiguration的扫描配置

因为springboot在项目启动的过程中会去扫描项目和所有项目依赖引用的jar包 扫描路径为类路径下的META-INF目录下的 spring.factories

配置读取所有的拦截器,过滤器,自动配置XXXAutoConfiguration 等等

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
 com.dhy.atuoConfiguration.HelloServiceAutoConfiguration   

流程:

其他springboot 项目引用了启动器,因为启动器依赖自动配置模块,然后也会扫描自动配置模块的类路径下的META-INF目录下的 spring.factories

HelloServiceAutoConfiguration配置类就会被拿到,然后里面的 helloService() 方法返回的HelloService对象就会被创建并且被@Bean 注解注册到ioc容器里面,这样 springboot 项目 里面就可以 通过@Autowired 注解使用 HelloService 对象了。

根springBoot自动配置原理一样,只有当用户没有在容器中放入自己自定义的HelloService时,才会在容器中放入对应的HelloService 对象


5.打包并在其它项目上面引入启动器

使用maven 的 install 分别顺序 的把 自动配置模块项目 和 启动器 项目 安装到你的本机maven仓库里面

(应为启动器是依赖自动配置模块的,所以先install 自动配置模块再install 启动器)

(如果你的启动器是给开发组用的,最好把 配置模块项目 和 启动器 项目 安装到相应的maven私服仓库就行,这样别的项目引用直接引用启动器就可以了)


6.测试使用

只需要在springboot的项目里添加启动器依赖

  <dependency>
            <groupId>com.dhy</groupId>
            <artifactId>dhy-hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

编写一个测试登录入口:

@RestController
public class UsrController
{

    @Autowired
    service s;
    @GetMapping("/show")
    public String show()
    {
        return s.sayHello("大忽悠");
    }
}

在全局配置文件中配置相关前缀属性:

dhy:
  hello:
   prefix: 你好
   suffix: 和小朋友