实战|如何自定义SpringBoot Starter?

栏目: Java · 发布时间: 5年前

内容简介:我在「SpringBoot 项目就是由一个一个 Starter 组成的,一个 Starter 代表该项目的 SpringBoot 起步依赖,除了官方已有的 Starter,如果你需要将自己的项目支持 SpringBoot,那么就需要把它制作成一个 Starter。这篇博客依据 SpringBoot 的自动化配置原理,开发一个属于自己的 Starter。自动化配置需满足两个条件:

我在「 SpringBoot自动化配置源码分析 」从源码的角度讲解了 SpringBoot 自动化配置的原理,知道了它最终要干的事情不过是读取 META-INF/spring.factories 中的自动化配置类而已。

SpringBoot 项目就是由一个一个 Starter 组成的,一个 Starter 代表该项目的 SpringBoot 起步依赖,除了官方已有的 Starter,如果你需要将自己的项目支持 SpringBoot,那么就需要把它制作成一个 Starter。这篇博客依据 SpringBoot 的自动化配置原理,开发一个属于自己的 Starter。

自定义 Starter

自动化配置需满足两个条件:

1. 能够生成 Bean,并注册到 Bean 容器中; 2. 能够自动配置项目所需要的配置。

在这里创建一个 spring-boot-starter-helloworld 项目作为例子,实现以上两点。

引入 SpringBoot 自动化配置依赖:


 

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-autoconfigure</artifactId>

<version>1.5.9.RELEASE</version>

</dependency>

</dependencies>

spring-boot-starter-helloworld 只是作为例子演示自定义 starter 的过程,实现的功能很简单就是创建一个 HelloworldService 的,并配置 sayHello() 方法打印的语句。


 

public class HelloworldService {


private String words;


private String getWords() {

return words;

}


public void setWords(String words) {

this.words = words;

}


public String sayHello() {

return "hello, " + words;

}

}

创建属性类,prefix = "helloworld"代表该项目在属性文件中配置的前缀,即可以在属性文件中通过 helloworld.words=springboot,就可以改变属性类字段 words 的值了。


 

@ConfigurationProperties(prefix = "helloworld")

public class HelloworldProperties {

public static final String DEFAULT_WORDS = "world";


private String words = DEFAULT_WORDS;


public String getWords() {

return words;

}


public void setWords(String words) {

this.words = words;

}

}

创建自动化配置类,这个相当于就是一个普通的 Java 配置类,可以在这里创建 Bean,并可获得与 application.properties 属性文件相对应的属性类的 Bean。


 

// 相当于一个普通的 java 配置类

@Configuration

// 当 HelloworldService 在类路径的条件下

@ConditionalOnClass({HelloworldService.class})

// 将 application.properties 的相关的属性字段与该类一一对应,并生成 Bean

@EnableConfigurationProperties(HelloworldProperties.class)

public class HelloworldAutoConfiguration {


// 注入属性类

@Autowired

private HelloworldProperties hellowordProperties;


@Bean

// 当容器没有这个 Bean 的时候才创建这个 Bean

@ConditionalOnMissingBean(HelloworldService.class)

public HelloworldService helloworldService() {

HelloworldService helloworldService = new HelloworldService();

helloworldService.setWords(hellowordProperties.getWords());

return helloworldService;

}

}

在 META-INF 目录下创建 spring.factories,这个属性文件可重要啦,因为 SpringBoot 自动化配置最终就是要扫描 META-INF/spring.factories 来加载项目的自动化配置类。


 

# Auto Configure

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.objcoding.starters.helloworld.HelloworldAutoConfiguration

引用 Starter

为了引入 starter,我在这里再创建一个 spring-boot-starter-helloworld-sample 项目。

添加 spring-boot-starter-helloworld 起步依赖:


 

<dependency>

<groupId>com.objcoding</groupId>

<artifactId>spring-boot-starter-helloworld</artifactId>

<version>1.0-SNAPSHOT</version>

</dependency>

在 application.properties 中添加属性:

helloworld.words=springboot

在 SpringBoot 主程序中 注入 helloworldService


 

@RestController

@SpringBootApplication

public class HelloworldApplication {


@Autowired

private HelloworldService helloworldService;


@GetMapping("/")

public String sayHello() {

return helloworldService.sayHello();

}


public static void main(String[] args) {

SpringApplication.run(HelloworldApplication.class, args);

}

}

访问 http://localhost:8080/,打印以下结果:

实战|如何自定义SpringBoot Starter?

Demo源码地址: https://github.com/objcoding/spring-boot-starter-tutorial

推荐阅读:

SpringBoot自动化配置源码分析

Java并发之AQS源码分析(二)

实战|如何自定义SpringBoot Starter?

长按可以订阅

来都来了

点个在看再走呗?

实战|如何自定义SpringBoot Starter?


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

司法的过程

司法的过程

(美)亨利·J.亚伯拉罕 / 泮伟江 宦盛奎 韩阳 / 北京大学出版社 / 2009-07-28 / 58.00元

本书是以比较研究的方法来分析司法哲学的经典文本之一。作者以敏锐的眼光透视了司法过程背后的理论、实践和参与其中的人。比较了美国、英国、法国的具体法院运作,审视了“司法能动主义”和“司法克制主义”之间的争辩。本书第七版的介绍吸收了美国、英国、法国和欧洲法院体系运作中的最新和重要的发展。 目前国内非常关注司法的运作过程、法官的裁判过程,此书的翻译对于这方面的研究很有助益,对于英国和法国法院的介绍填补了国......一起来看看 《司法的过程》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具