SpringBoot Starter原理详解,如何实现?

2026-05-26 04:561阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1150个文字,预计阅读时间需要5分钟。

SpringBoot Starter原理详解,如何实现?

1. Mybatis 自定义配置解析在我們自定義 starter 之前,我們先理解一下 Mybatis 及其配置。Mybatis 是一個流行的持久層框架,它允許開發者以 XML 或注解的方式配置 SQL 映射,進行數據庫操作。

2. Mybatis Starter 实现及依赖引入在 Spring Boot 引入 Mybatis 的過程中,我們需要添加相應的依賴。以下是在 Spring Boot 中引入 Mybatis 的依賴配置:

xmldependency groupId: org.mybatis.spring.boot artifactId: mybatis-spring-boot-starter

1、Mybatis 自定义配置的分析

在我们自定义starter之前我们写了解一下Mybatis 是如何实现starter

在SpringBoot 引入的依赖如下:

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>

mybatis的maven 依赖,主要涉及到的内容,spring.factories、MybatisAutoConfiguration、MybatisProperties

我们来看一下 META-INF/spring.factories文件,这个文件是以Map 形式存放的。key是EnableAutoConfiguration类的全类名,

value是一个MybatisAutoConfiguration,这就是当项目启动自动配置的类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

MybatisAutoConfiguration

@Configuration //标示是一个配置类

@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) //表示当SqlSessionFactory,SqlSessionFactoryBean存在这个配置类才生效。

@EnableConfigurationProperties({MybatisProperties.class}):就是把 MybatisProperties加入到 IOC 容器中。

MybatisProperties

对于@ConfigurationProperties注解它的作用就是把全局配置文件中的值绑定到实体类JavaBean上面(将配置文件中的值与MybatisProperties绑定起来),而@EnableConfigurationProperties主要是把以绑定值JavaBean加入到spring容器中。

分析完这些规则后,我们再来看看mybatis自定义的starter 的项目结构,主要是分为两个项目(一个是空项目(mtbatis-spring-boot-starter),一个是具体的实现自定义配置的项目(mybatis-spring-boot-autoconfigure)),空项目只是引入自定义配置项目的依赖,而实现映入的时候我们只需要映入空项(mtbatis-spring-boot-starter)即可。

到此我们已经分析完mybatis 自定义的starter,下面我们自己来实现一个自定义的starter。

2、自定义starter的实现

项目结构展示:

首先我们先定义一个 zfauto-spring-boot-autoconfigure 工程

编写属性类:添加 @ConfigurationProperties注解和前缀 zf.auto。之后我们就可以在 application.properties或application.yml 中 使用 zf.auto=指定参数了,由于篇幅的原因省略setter getter方法,实际是需要的,不然无法注入;

@ConfigurationProperties(prefix = "zf.auto") public class HelloProperties { private String prefix; private String suffix; }

编写配置类:加入@Configuration注解,@ConditionalOnWebApplication是web 应用配置类才起作用,以及 @EnableConfigurationProperties(HelloProperties.class) 注解,将属性注入到 IOC 容器中。

@Configuration @ConditionalOnWebApplication @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService(){ HelloService helloService=new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; } }

编写 spring.factories 文件:在resources路径下面创建META-INF,文件夹,然后创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zfauto.starter.HelloServiceAutoConfiguration

然后我们在创建一个空项目(zfauto-spring-boot-starter),在这个项目中我们引入zfauto-spring-boot-autoconfigure依赖

<dependency> <groupId>com.zfauto.starter</groupId> <artifactId>zfauto-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

HelloService 实现的功能,省略setter,getter的方法(实际需要)

public class HelloService { HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix()+ ","+name+","+helloProperties.getSuffix(); } }

最后我们 分别将项目打包,由于zfauto-spring-boot-starter是依赖于zfauto-spring-boot-autoconfigure,所以我们先对zfauto-spring-boot-autoconfigure进行打包,然后通过 mvn install 打到本地仓库(如何打包见下图)。

到此我们自定义的类实现。那我们来测试一下,这个和我们引入其他的starter一样了。

创建项目zfauto-spring-boot-starter-test ,引入自定义starter的依赖。

<dependency> <groupId>com.zfauto.starter</groupId> <artifactId>zfauto-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

application.properties中的配置如下

zf.auto.prefix=hello

zf.auto.suffix=123

具体的测试类

@RestController public class HelloController { @Autowired HelloService helloService; @RequestMapping("/sayHello") public String sayHello(){ return helloService.sayHello("小福子"); } }

项目访问路径:localhost:8080/sayHello

SpringBoot Starter原理详解,如何实现?

好了 ,本文就说到这里,本文相关案例我已经上传到 码云 ,小伙伴们可以自行下载:gitee.com/xiaofuzi123/springboot-learning-example

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1150个文字,预计阅读时间需要5分钟。

SpringBoot Starter原理详解,如何实现?

1. Mybatis 自定义配置解析在我們自定義 starter 之前,我們先理解一下 Mybatis 及其配置。Mybatis 是一個流行的持久層框架,它允許開發者以 XML 或注解的方式配置 SQL 映射,進行數據庫操作。

2. Mybatis Starter 实现及依赖引入在 Spring Boot 引入 Mybatis 的過程中,我們需要添加相應的依賴。以下是在 Spring Boot 中引入 Mybatis 的依賴配置:

xmldependency groupId: org.mybatis.spring.boot artifactId: mybatis-spring-boot-starter

1、Mybatis 自定义配置的分析

在我们自定义starter之前我们写了解一下Mybatis 是如何实现starter

在SpringBoot 引入的依赖如下:

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>

mybatis的maven 依赖,主要涉及到的内容,spring.factories、MybatisAutoConfiguration、MybatisProperties

我们来看一下 META-INF/spring.factories文件,这个文件是以Map 形式存放的。key是EnableAutoConfiguration类的全类名,

value是一个MybatisAutoConfiguration,这就是当项目启动自动配置的类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

MybatisAutoConfiguration

@Configuration //标示是一个配置类

@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) //表示当SqlSessionFactory,SqlSessionFactoryBean存在这个配置类才生效。

@EnableConfigurationProperties({MybatisProperties.class}):就是把 MybatisProperties加入到 IOC 容器中。

MybatisProperties

对于@ConfigurationProperties注解它的作用就是把全局配置文件中的值绑定到实体类JavaBean上面(将配置文件中的值与MybatisProperties绑定起来),而@EnableConfigurationProperties主要是把以绑定值JavaBean加入到spring容器中。

分析完这些规则后,我们再来看看mybatis自定义的starter 的项目结构,主要是分为两个项目(一个是空项目(mtbatis-spring-boot-starter),一个是具体的实现自定义配置的项目(mybatis-spring-boot-autoconfigure)),空项目只是引入自定义配置项目的依赖,而实现映入的时候我们只需要映入空项(mtbatis-spring-boot-starter)即可。

到此我们已经分析完mybatis 自定义的starter,下面我们自己来实现一个自定义的starter。

2、自定义starter的实现

项目结构展示:

首先我们先定义一个 zfauto-spring-boot-autoconfigure 工程

编写属性类:添加 @ConfigurationProperties注解和前缀 zf.auto。之后我们就可以在 application.properties或application.yml 中 使用 zf.auto=指定参数了,由于篇幅的原因省略setter getter方法,实际是需要的,不然无法注入;

@ConfigurationProperties(prefix = "zf.auto") public class HelloProperties { private String prefix; private String suffix; }

编写配置类:加入@Configuration注解,@ConditionalOnWebApplication是web 应用配置类才起作用,以及 @EnableConfigurationProperties(HelloProperties.class) 注解,将属性注入到 IOC 容器中。

@Configuration @ConditionalOnWebApplication @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; @Bean public HelloService helloService(){ HelloService helloService=new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; } }

编写 spring.factories 文件:在resources路径下面创建META-INF,文件夹,然后创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zfauto.starter.HelloServiceAutoConfiguration

然后我们在创建一个空项目(zfauto-spring-boot-starter),在这个项目中我们引入zfauto-spring-boot-autoconfigure依赖

<dependency> <groupId>com.zfauto.starter</groupId> <artifactId>zfauto-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

HelloService 实现的功能,省略setter,getter的方法(实际需要)

public class HelloService { HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix()+ ","+name+","+helloProperties.getSuffix(); } }

最后我们 分别将项目打包,由于zfauto-spring-boot-starter是依赖于zfauto-spring-boot-autoconfigure,所以我们先对zfauto-spring-boot-autoconfigure进行打包,然后通过 mvn install 打到本地仓库(如何打包见下图)。

到此我们自定义的类实现。那我们来测试一下,这个和我们引入其他的starter一样了。

创建项目zfauto-spring-boot-starter-test ,引入自定义starter的依赖。

<dependency> <groupId>com.zfauto.starter</groupId> <artifactId>zfauto-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

application.properties中的配置如下

zf.auto.prefix=hello

zf.auto.suffix=123

具体的测试类

@RestController public class HelloController { @Autowired HelloService helloService; @RequestMapping("/sayHello") public String sayHello(){ return helloService.sayHello("小福子"); } }

项目访问路径:localhost:8080/sayHello

SpringBoot Starter原理详解,如何实现?

好了 ,本文就说到这里,本文相关案例我已经上传到 码云 ,小伙伴们可以自行下载:gitee.com/xiaofuzi123/springboot-learning-example

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。