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,以后要用直接引用就行了~ 这样写代码是不是会有不一样的体验呢?

目录
相关文章
|
19天前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
|
2月前
|
缓存 NoSQL Java
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
Spring Cache 是 Spring 提供的简易缓存方案,支持本地与 Redis 缓存。通过添加 `spring-boot-starter-data-redis` 和 `spring-boot-starter-cache` 依赖,并使用 `@EnableCaching` 开启缓存功能。JetCache 由阿里开源,功能更丰富,支持多级缓存和异步 API,通过引入 `jetcache-starter-redis` 依赖并配置 YAML 文件启用。Layering Cache 则提供分层缓存机制,需引入 `layering-cache-starter` 依赖并使用特定注解实现缓存逻辑。
349 1
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
|
2月前
|
Java 数据安全/隐私保护 Spring
揭秘Spring Boot自定义注解的魔法:三个实用场景让你的代码更加优雅高效
揭秘Spring Boot自定义注解的魔法:三个实用场景让你的代码更加优雅高效
|
2月前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
2月前
|
安全 Java 数据安全/隐私保护
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
本文介绍了一个基于SpringBoot、Spring Security和JPA开发的校园图书管理系统,包括系统的核心控制器`LoginController`的代码实现,该控制器处理用户登录、注销、密码更新、角色管理等功能,并提供了系统初始化测试数据的方法。
36 0
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
|
2月前
|
安全 Java 数据库
|
2月前
|
JSON 安全 Java
|
2月前
|
监控 安全 Java
【开发者必备】Spring Boot中自定义注解与处理器的神奇魔力:一键解锁代码新高度!
【8月更文挑战第29天】本文介绍如何在Spring Boot中利用自定义注解与处理器增强应用功能。通过定义如`@CustomProcessor`注解并结合`BeanPostProcessor`实现特定逻辑处理,如业务逻辑封装、配置管理及元数据分析等,从而提升代码整洁度与可维护性。文章详细展示了从注解定义、处理器编写到实际应用的具体步骤,并提供了实战案例,帮助开发者更好地理解和运用这一强大特性,以实现代码的高效组织与优化。
54 0
|
2月前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
2月前
|
存储 Java API
下一篇
无影云桌面