SpringBoot——自定义一个spring-boot-starter包

简介: SpringBoot——自定义一个spring-boot-starter包

一句话总结:

1、编写自己的properties类(用来加载属性文件进行默认的配置)和核心服务类(要自动配置的bean)

2、自定义AutoConfiguration 配置类CustomerAutoConfiguration ,通过@Condition*系列注解控制自动配置的条件。

3、然后在src/main/resources新建文件夹META-INF,然后新建一个spring.factories文件

在里面使用org.springframework.boot.autoconfigure.EnableAutoConfiguration指定我们自定义的自动配置类的全路径。

这样,一个自定义的spring-boot-start工程就完成了。


命名规则:

这里啰嗦一下,官方对 Starter 包定义的 artifactId 是有要求的,当然也可以不遵守(手动滑稽)。

Spring 官方 Starter 通常命名为 spring-boot-starter-{name}如:spring-boot-starter-web,

比如:spring-cloud-starter-openfeign

Spring 官方建议非官方的 Starter 命名应遵守 {name}-spring-boot-starter 的格式。

比如:mybatis-spring-boot-starter


先简单分析下mybatis-spring-boot-starter的结构:

父包:mybatis-spring-boot 核心功能模块

子包:

mybatis-spring-boot-starter
mybatis-spring-boot-autoconfigure

29.png

1、发现mybatis-spring-boot-starter工程本身是个空项目,只通过pom文件和spring.provides说明依赖关系;

其中spring.provides是依赖文件jar包配置的artifactId值


2、在mybatis-spring-boot-autoconfigure工程中发现spring.facoties文件,

内容为:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration


核心代码中只有三个类文件:

1、MybatisAutoConfiguration 自动配置类

2、MybatisProperties 属性文件加载类,通过@EnableConfigurationProperties({MybatisProperties.class})注入到spring容器中,使得在MybatisAutoConfiguration中可以直接注入使用配置属性

3、SpringBootVFS 是类扫描器

打包成jar时,setTypeAliasesPackage(“xxx”)找不到类的问题。MyBatis通过VFS来扫描,

在Spring Boot中由于是嵌套Jar,导致Mybatis默认的VFS实现DefaultVFS无法扫描嵌套Jar中的类,需要改成SpringBootVFS扫描。

27.png


28.png



通过分析总结一些基础步骤:

本文demo参考mybatis-spring-boot-starter和网上的文章创建自己的start工程。

定义我们自己的spring boot starter工程,通常需要以下4个工程:


模块的真正的实现类,如本文的工程为first-spring-boot/-spring的工程:此工程是-spring-boot-autoconfigure和*-spring-boot-starter的父工程类。本例子里是first-spring-boot。

此工程的命名规则如下:如果你在这个工程中引入spring依赖,则工程名称为*-spring,如果引入spring-boot依赖,则工程名称为*-spring-boot。

*-spring-boot-autoconfigure工程:配置自动配置类,本例的名称first-spring-boot-autoconfigure

*-spring-boot-starter工程:空工程类,在pom.xml中定义需要引入jar,本例的名称first-spring-boot-starter

为了能更好的理解Springboot的自动配置和工作原理,我们今天来手写一个spring-boot-hello-starter。这个过程很简单,代码不多。接下来我们看看怎么开始实践。


1. 新建maven工程。


这块就不演示了,如果不会可以自行百度…啦啦啦,因为太简单了啊


2.新建一个properties类


/**
 * @author Lee
 * @// TODO 2018/7/25-9:21
 * @description
 */
@ConfigurationProperties(prefix = "customer")
public class CustomerProperties {
    private static final String DEFAULT_NAME = "Lensen";
    private String name = DEFAULT_NAME;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


3.创建一个服务类CustomerService


/**
     * @author Lee
     * @// TODO 2018/7/25-10:30
     * @description
     */
    public class CustomerService {
        private String name;
    public String findCustomer(){
        return "The Customer is " + name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


4.AutoConfiguration类


/**
 * @author Lee
 * @// TODO 2018/7/25-10:32
 * @description
 */
@Configuration
@EnableConfigurationProperties(CustomerProperties.class)
@ConditionalOnClass(CustomerService.class)
@ConditionalOnProperty(prefix = "customer", value = "enabled", matchIfMissing = true)
public class CustomerAutoConfiguration {
    @Autowired
    private CustomerProperties customerProperties;
    @Bean
    @ConditionalOnMissingBean(CustomerService.class)
    public CustomerService customerService() {
        CustomerService customerService = new CustomerService();
        customerService.setName(customerProperties.getName());
        return customerService;
    }
}

核心注解说明:


@Configuration声明是配置类


@EnableConfigurationProperties(CustomerProperties.class)

将被 @ConfigurationProperties修饰的类CustomerProperties加载到spring容器中

@ConditionalOnClass(CustomerService.class)

只有在项目路径下加载到CustomerService类,才会进行自动配置

@ConditionalOnProperty(prefix = “customer”, value = “name”,matchIfMissing = true)

检查配置文件中,是否有以customer开头名称为enabled的配置, 由于允许属性缺失,所以仍然会自动配置。


5. spring.factories配置


在src/main/resources新建文件夹META-INF,然后新建一个spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.developlee.configurer.CustomerAutoConfiguration

pom文件修改artifactId为spring-boot-hello-starter 打包成jar. 然后用mvn install 安装到本地mvn仓库。


测试spring-boot-hello-start

新建springboot工程,在pom.xml文件引入安装的jar包

com.developlee spring-boot-hello-starter 1.0-SNAPSHOT

在项目启动类中直接做测试:

@SpringBootApplication
@RestController
public class TestStarterApplication {
@Autowired
CustomerService customerService;
@GetMapping("/")
public String index(){
    return customerService.getName();
}
public static void main(String[] args) {
    SpringApplication.run(TestStarterApplication.class, args);
}
}

application.properties文件配置下我们的customer.name

customer.name = BigBBrother

接下来启动项目,请求地址localhost:8080,即可看到页面上显示BigBBrother。


到这我们已经完成了一个spring-boot-starter jar包的开发,有很多功能我们可以自己封装成一个jar,以后要用直接引用就行了~ 这样写代码是不是会有不一样的体验呢?

目录
相关文章
|
30天前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
30天前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
1月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
316 2
|
2月前
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
1488 1
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
存储 JSON Java
444 0
|
2月前
|
监控 安全 Java
使用 @HealthEndpoint 在 Spring Boot 中实现自定义健康检查
Spring Boot 通过 Actuator 模块提供了强大的健康检查功能,帮助开发者快速了解应用程序的运行状态。默认健康检查可检测数据库连接、依赖服务、资源可用性等,但在实际应用中,业务需求和依赖关系各不相同,因此需要实现自定义健康检查来更精确地监控关键组件。本文介绍了如何使用 @HealthEndpoint 注解及实现 HealthIndicator 接口来扩展 Spring Boot 的健康检查功能,从而提升系统的可观测性与稳定性。
196 0
使用 @HealthEndpoint 在 Spring Boot 中实现自定义健康检查
|
2月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
|
3月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
618 3