Springboot开发自定义starter

我们要想自定义starter,首先要了解springboot是如何加载starter的,也就是springboot的自动装配机制

starter加载原理:

springboot通过一个@SpringBootApplication注解启动项目,springboot在项目启动的时候,会将项目中所有声明为Bean对象(注解、xml)的实例信息全部加载到ioc容器当中。 除此之外也会将所有依赖到的starter里的bean信息加载到ioc容器中,从而做到所谓的零配置,开箱即用。

在@SpringBootApplication注解中有个@EnableAutoConfiguration注解,@EnableAutoConfiguration进行加载starter

我们进入@EnableAutoConfiguration注解中发现具体的加载实现是由@EnableAutoConfiguration注解下import了一个AutoConfigurationImportSelector加载器实现

这个AutoConfigurationImportSelector会去所引用的依赖jar包下,找到一个”spring.factories”文件,一般spring.factories文件里都会声明该依赖所提供的核心功能bean配置信息。文件一般在依赖jar包的META-INF文件夹下面

找到后将这个文件里声明的配置信息进行加载,从而达到加载全部bean信息的效用。

这就是自定义springboot加载starter原理,其实就是加载依赖jar包下的spring.factories文件。所以我们要自定义starter,就需要在项目中建立一个META-INF的文件夹,然后在文件夹下面建一个spring.factories文件,文件里将你需要提供出去的bean实例信息配置好就行。

自定义starter:

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

常见场景:

1.通用模块-短信发送模块

2.基于AOP技术实现日志切面

4.微服务项目的数据库连接池配置

5.微服务项目的每个模块都要访问redis数据库,每个模块都要配置redisTemplate也可以通过starter解决

自动加载核心注解说明:

自定义starter代码:

新建springboot项目。简单演示所以需求配置任务依赖。如springboot构建很慢,或者打包的时候下载依赖很慢,可在pom文件中添加如下配置。可以加快构建速度

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>
    <groupId>cn.itcast</groupId>
    <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot-starter-autoconfigure</name>
    <description>Demo project for Spring Boot</description>
    <!--自动配置包-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

项目构建完成后,在resources文件夹下面新建META-INF文件夹,并新建spring.factories文件

因为我们是作为提供服务模块,所以不需要启动类,删除启动类并新建服务提供类、属性类和自动配置类

编写相关属性类(XxxProperties):

@ConfigurationProperties注解基本用法

1、前缀定义了哪些外部属性将绑定到类的字段上

2、根据 Spring Boot 宽松的绑定规则,类的属性名称必须与外部属性的名称匹配

3、我们可以简单地用一个值初始化一个字段来定义一个默认值

4、类本身可以是包私有的

5、类的字段必须有公共 setter 方法

注意:HelloProperties代码写完后会报如下错误,这是正常的,因为还有配置类AutoConfig和一个注解@EnableConfigurationProperties没有加(Not registered via @EnableConfigurationProperties or marked as Spring component)

package cn.itcast.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("itcast.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;
    }
}

编写Starter项目服务提供类:

package cn.itcast.service;

import cn.itcast.bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 默认不要放在容器中
 */
public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public String sayHello(String userName){
        return  helloProperties.getPrefix()+":"+userName+">"+helloProperties.getSuffix();
    }


}

编写自动配置类:

@Configuration:

定义一个配置类

@EnableConfigurationProperties:

@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。

如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的

package cn.itcast.auto;

import cn.itcast.bean.HelloProperties;
import cn.itcast.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)//默认放在容器中
public class HelloServiceAutoConfigure {

    @Bean
    @ConditionalOnMissingBean(HelloService.class)//没有才创建
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        return helloService;
    }
}

编写spring.factories文件加载自动配置类:

在resources资源文件夹下新建META-INF文件夹,然后创建spring.factories文件

在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.itcast.auto.HelloServiceAutoConfigure

注意:HelloServiceAutoConfigure是starter配置文件的类限定名,多个之间逗号分割,还可以\进行转义即相当于去掉后面换行和空格符号,如下所示

# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
打包安装:

配置完后就可以开始maven install安装到maven仓库中了

成功后,我们可以发现我们本地仓库就会有刚刚我们打包完的项目:

到这里为止,我们的整个自定义starter的准备工作就做完了,如下是我的整个自定义starter文件的结构:

在其他项目中的应用 :

首先在其他项目的pom.xml中引入相关依赖:

 <dependency>
    <groupId>cn.itcast</groupId>
    <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

在application.yml文件中添加配置

为了展示效果,我们写一个测试类,看能否看到我们想要的效果,测试代码如下:

package cn.itcast.controller;

import cn.itcast.auto.HelloServiceAutoConfigure;
import cn.itcast.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/sayhello")
    public String sayHello(){
        return helloService.sayHello("张三");
    }
}

启动项目后,http调用接口测试一下:

至此,我们就印证了如上所说的,sprinboot是通过加载META-INF文件夹下的spring.factories文件完成自动配置的功能以及开箱即用的效果的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值