自定义SpringBoot starter

什么时候需要创建自定义starter

在我们的日常开发中,可能会需要开发一个通用模块,以供其它工程复用。这时我们可以把通用模块封装成tarter,这样其它工程复用的时候只需在pom中引用依赖,由SpringBoot为我们完成自动装配。

怎么样定义starter

通过Springboot的AutoConfiguration机制,尝试自定义SpringBoot starter。

了解springboot是如何加载starter的,也就是Springboot自动装配机制

自定义starter的命名规范

spring-boot-starter-xxx是SpringBoot官方定义的jar,如spring-bbot-starter-web。

xxx-spring-boot-starter是非官网定义的,如第三方jar包druid-spring-boot-starter。

参考第三方starter自定义个人starter

以druid-spring-boot-starter为例

  1. 当导入了 druid-spring-boot-starter后,其导入的相关依赖如下:

  1. 可以看到其导入了 druid-spring-boot-autoconfigure的jar包,其中有一个自动配置类DruidDataSourceAutoConfigure ,如图

  1. 注解@Configuration和@Bean注解,其结合使用可以替代传统的xml配置文件,@Bean把DataSource注入到Spring容器。

注解@EnableConfigurationProperties,其作用是让后面指定的配置属性生效。

注解@ConditionalOnClass,当项目中存在某个类时才会使标有该注解的类或方法生效;

注解@AutoConfigureBefore,该注解的值会在标有该注解的类之前执行;

注解@Import,导入配置类;

  1. 在SpringBoot进行自动装配时,它会选择性的读取META-INF/spring.factories配置文件。

其中key是固定的,value是指定配置类的全路径。

自定义我的starter

1、创建一个空的maven工程

2、引入pom依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

3、读取并注入配置信息

  1. 创建MqProperties配置属性类

package org.intl.util;

import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello")
public class MqProperties {
    private String mqHost="localhost";
    private String mqUsername="root";
    private String mqPassword="123456";

    public String getMqHost() {
        return mqHost;
    }

    public void setMqHost(String mqHost) {
        this.mqHost = mqHost;
    }

    public String getMqUsername() {
        return mqUsername;
    }

    public void setMqUsername(String mqUsername) {
        this.mqUsername = mqUsername;
    }

    public String getMqPassword() {
        return mqPassword;
    }

    public void setMqPassword(String mqPassword) {
        this.mqPassword = mqPassword;
    }
}
  1. 创建MqService服务类

package org.intl.service;

import org.intl.util.MqProperties;
import org.springframework.beans.factory.annotation.Autowired;


public class MqService {
    @Autowired
    private MqProperties mqProperties;

    public String getMq() {
        return mqProperties.getMqHost() + "," + mqProperties.getMqUsername() + "," + mqProperties.getMqPassword();
    }
}
  1. 创建MqAutoConfiguration自动配置类,注入MqService到Spring中。

package org.intl.config;

import org.intl.service.MqService;
import org.intl.util.MqProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableConfigurationProperties(MqProperties.class)
public class MqAutoConfiguration {

    @Bean
    public MqService mqService(){
        return new MqService();
    }
}
  1. 在resources目录下,创建文件META-INF\spring.factories,指定自动配置类的路径

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.intl.config.MqAutoConfiguration
  1. 项目打包发布到maven仓库

引入自定义starter

  1. 新建一个SpringBoot的项目,导入依赖

  1. 配置文件进行配置

  1. 编写测试接口

  1. 启动运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值