实现自定义Spring Boot Starter


Spring Boot 可以省略众多的繁琐配置,它的众多 starter 可以说是功不可没。 Spring Boot默认定义了很多的starter(spring-boot-starter-xxx),这些starter的简化了很多烦琐的配置,通过引入各种 Spring Boot Starter 包可以快速搭建出一个项目的脚手架。常用的 starter 还有 spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-jdbc,相比于传统的 xml 配置可以说是大大减少了集成的工作量。不过也是不可能应付所有需求的,根据我们自己的业务,我们可以自定义 starter 来实现项目中复用度高的业务,让别的模块能很方便的引入使用。

一、原理

利用 starter 实现自动化配置只需要两个条件:maven 依赖和配置文件,这里简单介绍下starter 实现自动化配置的流程。
引入 maven 依赖实质上就是导入 jar 包,Spring Boot 项目启动的时候会找到 starter jar 包中的 resources/META-INF/spring.factories 文件,根据 spring.factories 文件中的配置,加载需要自动配置的类。
以 mybatis-spring-boot-starter 为例,其自动配置类之一如下:

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties({MybatisProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})
public class MybatisAutoConfiguration implements InitializingBean {
    ...
}        

对 MybatisAutoConfiguration 中用到的注解的说明,参考如何实现自定义的 Spring Boot Starter
在这里插入图片描述
mybatis-spring-boot-starter 的 pom 文件引用了 mybatis-spring-boot-autoconfigure 依赖,其中的 spring.factories 文件配置如下:

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

这样,便可以在 Spring Boot 项目启动的时候会加载 mybatis starter 中的 MybatisLanguageDriverAutoConfiguration 和 MybatisAutoConfiguration 类。

二、实战

首先实现自定义的 spring-boot-starter;其次,通过一个新的工程 来引入该 starter,测试自定义starter是否生效。

1 自定义 Spring Boot Starter

功能描述:此starter供使用时获取到配置文件的属性值,作为业务逻辑的参数实现相关功能。

1.1 添加maven依赖

这里说下artifactId的命名问题:Spring 官方 Starter 通常命名为 spring-boot-starter-{name},如 spring-boot-starter-web;非官方 Starter 命名应遵循{name}-spring-boot-starter的格式

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

1.2 属性类AuthorProperties

用于获取配置文件前缀为custom的value值

@ConfigurationProperties(prefix = "custom")
public class AuthorProperties {
    public static final String DEFAULT_AUTHOR = "zsh";
    public String author = DEFAULT_AUTHOR;
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

1.3 自动配置类AuthorAutoConfiguration

@Configuration
@ConditionalOnClass({ AuthorServer.class })
@EnableConfigurationProperties(AuthorProperties.class)
public class AuthorAutoConfiguration {
    @Resource
    private AuthorProperties authorProperties;
    @Bean
    @ConditionalOnMissingBean(AuthorServer.class)
    @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
    public AuthorServer authorResolver() {
        AuthorServer authorServer = new AuthorServer();
        authorServer.setAuthor(authorProperties.getAuthor());
        return authorServer;
    }
}

@ConditionalOnClass 表示有 ExampleService 类存在时执行。
@EnableConfigurationProperties 表示有 ExampleProperties 类存在时执行。
@ConditionalOnMissingBean 当 ExampleService Bean 缺失时,执行下段代码。
@ConditionalOnProperty 当 example.service.enabled = true 时,执行代码。

1.4 业务逻辑AuthorServer

public class AuthorServer {
    public String author;
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

1.5 spring.factories

在 resources 目录下新建 META_INF 文件夹,并在其中新建 spring.factories,在其中写入:

# CUSTOM
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zsh.AuthorAutoConfiguration

至此,Starter 项目创建完成,使用 mvn install 命令打包。
在这里插入图片描述
出现BUILD SUCCESS代表打包成功!
在这里插入图片描述

2 测试自定义的 Spring Boot Starter

2.1 新建module或者新建工程

在pom文件中引入自定义的Spring Boot Starter

<dependency>
			<groupId>com.zsh</groupId>
			<artifactId>zsh-spring-boot-starter</artifactId>
			<version>1.0-SNAPSHOT</version>
</dependency>

2.2 创建controller

@RestController
@RequestMapping("/userController")
public class UserController {

	@Autowired
	private UserService userService;


	@Autowired
	private AuthorServer authorServer;

	//获取自定义stater方法
	@RequestMapping("/author")
	String home() {
		return "发布者:"+ authorServer.getAuthor();
	}
}	

2.3 yml配置文件

custom:
  author: hello-world
  enabled: true

2.4 启动项目

http请求结果如下,代表新工程调用自定义starter成功!

localhost:8086/userController/author

在这里插入图片描述

2.5 项目整体结构及源码

源码下载,欢迎star!SpringBoot2.XLearn
在这里插入图片描述
参考资料
springboot自定义和使用starter
【企业级解决方案一】编写一个自己的SpringBootStarter
springboot如何自定义starter
如何实现自定义的 Spring Boot Starter?

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值