SpringBoot实践(三十八):自定义spring-boot-starter
创始人
2024-05-10 07:35:47
0次

目录

自动配置原理

 自定义starter包

导入springboot的自动配置依赖 

测试业务代码

spring.factories配置

​编辑

 本地包上传

使用自定义starter依赖

测试和配置


自动配置原理

基于springBoot的starter机制能够让我们在使用外部包时候非常方便,只需要引入该组件提供的starter包即可,比如在pom中引入mybatis的mybatis-plus-boot-starter依赖包,那么springboot启动时候会自动扫描所有的依赖该包下META-INF文件夹下的spring.factories把自动配置类加入容器:

在spring.factories中会指定自动配置类:

# Auto Configure
org.springframework.boot.env.EnvironmentPostProcessor=\com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

其中MybatisPlusAutoConfiguration.java中会初始化若干的配置,在MybatisPlusProperties.class文件中,还有其他一些关于jdbc连接的条件注解;

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties({MybatisPlusProperties.class})

 自定义starter包

测试自定义starter包,该starter包中引入springboot的自动配置依赖,配置spring.factories文件中的自动加载类,实现的功能是在测试的springBoot工程中引入自定义的starter包的pom依赖后,能够调用starter包中的某些实例方法,具体而言是读取application.properties或者application.yml的配置输出;

首先创建1个空的项目,作为自定义starter包和测试的springboot工程的父工程,添加2个子模块:

导入springboot的自动配置依赖 

创建spring初始化器生成的子模块test01-spring-boot-starter,跟mybatis类似,或者直接copy,增加spring-boot-starter的依赖管理(不引入),只引入1个自动配置依赖;

	org.springframework.bootspring-boot-dependencies2.3.7.RELEASEpomimportorg.springframework.bootspring-boot-autoconfigurecompile

测试业务代码

创建1个属性配置类TestProperties.java,读取application.properties中的配置:

package com.example.hello.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "test.hello")
public class TestProperties {String prefix;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;}
}

 创建业务代码,实现模拟的业务需求,它使用上面的配置类读取配置做业务处理(直接输出);

package com.example.hello.service;
import com.example.hello.bean.TestProperties;
import org.springframework.beans.factory.annotation.Autowired;public class HelloService {@AutowiredTestProperties testProperties;public String sayHello() {return testProperties.getPrefix() + "<---- say hello --->" + testProperties.getSuffix();}}

再创建1个自动配置类TestAutoConfiguration.java,该类将被写入spring.factories中作为自动配置类在后面的测试springBoot工程启动中被扫描到IOC容器,该配置类将把上面的service服务实例化;

package com.example.hello.config;
import com.example.hello.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@ConditionalOnMissingBean(HelloService.class)
public class TestAutoConfiguration {@Beanpublic HelloService makeHelloService(){return new HelloService();}
}

spring.factories配置

在\resources\META-INF下创建spring.factories文本,指定当前测试stater包中的配置类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.hello.config.TestAutoConfiguration

工程目录如下: 

 本地包上传

以上就完成业务逻辑,使用pom的clean+install命令把该starter工程上传本地,其他程序可以直接通过pom引入;

使用自定义starter依赖

再创建1个子模块test-my-project,作为真正的依赖上面的业务代码实体,它是个web应用:

 编辑其pom依赖如下,这里的重点是test01-spring-boot-starter的依赖工程,是我们上面本地上传的包;

	org.springframework.bootspring-boot-dependencies2.3.7.RELEASEpomimportorg.springframework.bootspring-boot-autoconfigurecompileorg.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-webcom.exampletest01-spring-boot-starter0.0.1-SNAPSHOTorg.springframework.bootspring-boot-starter-testtest

测试和配置

当前工程的application.properties写入以下配置:

test.hello.prefix = prefix
test.hello.suffix = suffix

直接使用注解使用starter包中的HelloService这个bean,它将在启动时候被IOC实例化;

@RestController
public class HelloController {@AutowiredHelloService helloService;@GetMapping("/hello")public String sayHello(){return helloService.sayHello();}
}

输出正常表明测试的starter包被成功引入,并且实现了业务代码的类被IOC实例化,在真实的工程中可以直接调用。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...