SprinigBoot自定义Starter
自定义Starter
是什么
starter可以理解是一组封装好的依赖包,包含需要的组件和组件所需的依赖包,使得使用者不需要再关注组件的依赖问题
所以一个staerter包含
- 提供一个autoconfigure类
- 提供autoconfigure类的依赖
怎么做
创建starter大概需要
- 需要一个配置类bean,来填充配置
- 获取配置信息,注册到容器
- 将配置类加到自动配置
导入自动装配和Spring boot依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
创建一个bean
@Data
public class HelloworldService {
private String words;
public String sayHello() {
return "hello, " + words;
}
}
创建peoperties类来自定义需要的参数,用来接收properties或者yml里的配置
//helloword就是配置的前缀
@ConfigurationProperties(prefix = "helloword")
public class HelloworldProperties {
public static final String DEFAULT_WORDS = "world";
//这个值就是helloword.words
private String words = DEFAULT_WORDS;
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
}
创建configureation类来注册bean
将配置的参数注入到bean里,在注册到容器
@Configuration
//指定条件成立的情况下自动配置类生效(往哪装配)
@ConditionalOnClass(HelloworldService.class)
//让xxxProperties生效加入到容器中
@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;
}
}
在启动类里标明需要自动装配
@EnableAutoConfiguration//开启自动装配
@ComponentScan({"test"})
public class TestApplication {
}
最后是指定装配哪个配置类
在resources目录下创建META-INF文件夹
然后创建spring.factories标注需要装配的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
test.config.HelloworldAutoConfiguration
最后就可以用这个starter了
首先导入这个starter
<dependency>
<groupId>org.example</groupId>
<artifactId>test-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
然后导入HelloworldService这个bean
和在yml里配置参数
helloword:
words : hello
就完成了
SprinigBoot自定义Starter的更多相关文章
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
- Spring Boot 自定义 starter
一.简介 SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应 ...
- java框架之SpringBoot(10)-启动流程及自定义starter
启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...
- SpringBoot应用篇(一):自定义starter
一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...
- SpringBoot第十六篇:自定义starter
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 这一段时间 ...
- 小代学Spring Boot之自定义Starter
想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...
- (springboot)自定义Starter
要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- 对照谈-官方spring-boot-starter和自定义starter异同分析
在前面我讲用spring-boot-starter-mail发邮件的时候,我侧重看的是spring boot发邮件的便利性,今天,我们聊下另外一个方面,spring-boot-starter自身的结构 ...
随机推荐
- Sql获取表所有列名字段——select * 替换写法,Sqlserver、Oracle、PostgreSQL、Mysql
实际开发中经常用到select * from table,往往需要知道具体的字段,这个时候再去数据库中翻或者查看数据字典比较麻烦.为了方便,自己特意写了一个小函数f_selectall,针对SqlSe ...
- 获取ul中li的value值
<script> $(function(){ $(".month-list").find("li").click(function(){ var t ...
- linux修改静态ip
1.修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens32 bootproto:设置为静态 onboot:开机自启 ipaddr:ip地址 netma ...
- Python 速通爆肝、列表推导式、生成器、装饰器、生命游戏
列表推导式.赋值.切片(替换.插入).字符串处理与判断.enumerate().格式化字符串.读写文件.global 关键字.字符串startswith().类与对象.生成器.装饰器.Self.*ar ...
- NodeJS学习日报day4——模块化
// console.log(module); // 执行顺序不同,结果也不同 // module.exports = { // name : 'Cra2iTeT', // hi() { // con ...
- python中的嵌套
嵌套:将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套.既可以在列表中嵌套字典,也可以在字典中嵌套列表,甚至在字典中嵌套字典. 一.列表中嵌套字典 1)一般创建方式: student_ ...
- 项目依赖模块解决、二次封装Response、后台数据库配置、user模块user表设计、前台创建及配置
今日内容概要 二次封装Response 后台数据库配置 user模块user表设计 前台创建及配置 内容详细 补充--项目依赖模块 # 导出项目依赖模块和安装项目依赖模块 第三方模块--->导出 ...
- 企业应用架构研究系列二十八:身份认证 Beginning Out With IdentityServer4
在.Netcore 技术栈中,一直在使用了开源组件IdentityService4进行身份管理,其功能的强大和易用性的确很受开发者喜欢,但是最近其开源组织Duende Software 开始对其进行商 ...
- 学习HTTP——HTTPS
前言 因为工作需要,需要用到大量的关于 HTTP 协议的知识,目前掌握的关于 HTTP 请求以及协议的知识都是零散的,打算针对知识盲区系统的学习一些,理清概念. 为什么会出现 HTTPS 因为 HTT ...
- ubuntu 18及以上版本配置IP的方法,你get了吗
本文讲讲 Ubuntu 18 及以上版本配置 IP 的方法,为什么它值得一讲,因为以 Ubuntu 16 为首的版本的配置方法已经不适用了,如果你还不知道,那本文正好 get 一个新技能. Ubunt ...