做多站发布信息的网站,工信部的网站备案信息,wordpress joomla 菜单,wordpress phpwamp从前从前#xff0c;有个面试官问我一个 SpringBoot Starter 的开发流程#xff0c;我说我没有写过 starter#xff0c;然后就没有然后了#xff0c;面试官说我技术深度不够。
我想说这东西不是很简单吗#xff0c;如果要自己写一个出来也是分分钟的事情。至于就因为我没…从前从前有个面试官问我一个 SpringBoot Starter 的开发流程我说我没有写过 starter然后就没有然后了面试官说我技术深度不够。
我想说这东西不是很简单吗如果要自己写一个出来也是分分钟的事情。至于就因为我没有写过 starter 就觉得我一点都不会 SpringBoot 吗
当然我当时确实水平不足连 Java 的 SPI 都忘了是啥后来又捡了起来原来我在大学的时候就用过 Java 的 SPI悔之晚矣
什么是 SpringBoot starter
starter 是 SpringBoot 的一个重要的组成部分它相当于一个集成的模块比如你想用 Mybatis 和 lombok但是在 pom 文件中需要写两个依赖如果你将他们集成为一个 starter或者将更多你需要的依赖集成进去那么你只需要在 pom 文件中写一个 starter 依赖就可以了这对于一个可复用模块的开发和维护都极为有利。
同时在 maven 中引入 starter 依赖之后SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置它遵循“约定大于配置”的理念。
如何开发一个 SpringBoot starter
1、环境说明
jdk 1.8.0_151
maven 3.6.3
IDEA 编译器
要开发的 starter 是一个日期格式化工具它的功能是可以指定如何格式转化日期类型同时在配置文件中可以开启关闭此功能
2、创建项目
在 IDEA 中新建一个 maven 工程如下图所示。 Spring 官方建议自定义的 starter 使用 xxx-spring-boot-starter 命名规则以区分 SpringBoot 生态提供的 starter。
然后需要在 pom 文件中加入实现 starter 所需要的依赖。
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.walker.planes/groupIdartifactIddate-format-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/version
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactIdversion2.3.0.RELEASE/version/dependency/dependencies
/project
3、创建配置信息实体类
在开发 SpringBoot 项目的时候我们有时候会在 application.properties 配置文件中进行一些配置的修改用以开启或修改相关的配置如 server.port 可以用来修改项目启动的端口所以在编写自定义日期格式化 starter 时也可以用到这个功能根据配置文件来开启或关闭某项功能。
我们在 application.properties 配置文件中新增两个配置项第一个是用来控制该 starter 的启动或关闭第二个是需要指定的格式化信息。 然后创建一个配置文件映射类它的私有成员变量 pattern 就是需要在配置文件中指定的属性如果没有指定默认值为 yyyy-MM-dd HH:mm:ss。
import org.springframework.boot.context.properties.ConfigurationProperties;
/*** 配置信息实体类* author Planeswalker23* date 2022-12-25*/
ConfigurationProperties(formatter)
public class DateFormatProperties {
/*** default format pattern*/
private String pattern yyyy-MM-dd HH:mm:ss;
// 忽略 getter setter
}
ConfigurationProperties(prefix “formatter”) 注解的作用是将相同前缀的配置信息通过配置项名称映射成实体类。在这里就可以将 application.properties 配置文件中前缀是 formatter 的配置项映射到 DateFormatProperties 类中。
4、创建配置类
然后我们需要创建一个配置类这个配置类能够将核心功能类注入到 Ioc 容器使得在其他引用此 starter 的项目中能够使用自动配置的功能。
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.text.SimpleDateFormat;
/*** 配置信息实体类* author Planeswalker23* date 2022-12-25*/
Configuration
EnableConfigurationProperties(DateFormatProperties.class)
ConditionalOnProperty(prefix formatter, name enabled, havingValue true)
public class DateFormatConfiguration {private DateFormatProperties dateFormatProperties;public DateFormatConfiguration(DateFormatProperties dateFormatProperties){this.dateFormatProperties dateFormatProperties;}
Bean(name myDateFormatter)public SimpleDateFormat myDateFormatter() {System.out.println(start to initialize SimpleDateFormat with pattern: dateFormatProperties.getPattern());return new SimpleDateFormat(dateFormatProperties.getPattern());}
}
这里 starter 的功能是基于 SimpleDateFormat 类的其实就是解析配置文件中的指定格式将其设置为 SimpleDateFormat 的 pattern 属性然后将 SimpleDateFormat 类作为“组件”注册在 Ioc 容器中这样我们就可以在引用了该 starter 的项目中使用自定义格式化功能了。
Configuration 注解的作用是将 DateFormatConfiguration 类作为配置类注入容器
EnableConfigurationProperties 注解的作用是开启资源实体类的加载也就是说开启配置文件映射为资源实体类的功能。
ConditionalOnProperty 注解是开启条件化配置也就是说只有在配置文件中的 formatter.enabled 属性的值为 true 时这个 starter 才会生效。
5、指定自动装配
至此开发的业务代码就结束了但是还有一个最重要的步骤就是指定 DateFormatConfiguration 类作为自动装配类。
这需要在 resource 目录下创建 MATE-INF 文件夹并创建一个名为 spring.factories 的文件然后在文件中写下
org.springframework.boot.autoconfigure.EnableAutoConfigurationorg.walker.planes.DateFormatConfiguration这行代码的意思就是将 DateFormatConfiguration 设置为自动装配类在 SpringBoot 工程启动时会去扫描 MATE-INF 文件夹的 spring.factories 文件并加载这个文件中指定的类启动自动装配功能。
6、发布测试
然后在命令行中敲下 mvn clean install 命令当看到下面的输出后自定义的 starter 就可以作为一个依赖被引用了。
[INFO] -----------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------------------------
[INFO] Total time: 1.470 s
[INFO] Finished at: 2020-05-25T20:24:4908:00
[INFO] -----------------------------------------------------------新建一个测试工程在 pom 文件中加入如下的依赖。
dependencygroupIdorg.walker.planes/groupIdartifactIddate-format-spring-boot-starter/artifactIdversion1.0-SNAPSHOT/version
/dependency然后在 application.properties 文件中开启自定义 starter 的配置。 formatter.enabledtrue formatter.patternyyyy-MM-dd 然后在启动类中创建一个 Runner 任务简单的输出一个日期类就像下面这样。
SpringBootApplication
public class FirstStarterApplication implements ApplicationRunner {public static void main(String[] args) {SpringApplication.run(FirstStarterApplication.class, args);}Overridepublic void run(ApplicationArguments args) throws Exception{System.out.println(simpleDateFormat.format(new Date()));}Resource(type SimpleDateFormat.class)private SimpleDateFormat simpleDateFormat;
}启动项目我们可以看到自动配置生效了程序将配置文件中指定的格式化样式覆盖了默认的 pattern。
至此我们已经完成了一个简单的 SpringBoot Start 的开发了最后来总结一下一个 starter 的开发流程。
引入 spring-boot-autoconfigure 依赖创建配置实体类创建自动配置类设置实例化条件Conditionalxxx注解可不设置并注入容器在 MATE-INF 文件夹下创建 spring.factories 文件夹激活自动配置。在 maven 仓库发布 starter