SpringBoot(三)手写starter pom自动配置
思想:主要是EnableAutoConfiguration在启动的时候会扫描spring.factories并加载
1在resource下面新建META-INF/spring.factories
2在spring.factories中添加自动装载的类
3其他项目引用既OK
1.新建一个starter的Maven项目A,pom文件修改
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example.start</groupId>
- <artifactId>spring-boot-starter-demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demo</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <!--支持读取配置文件属性-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- <!--需要改jar 才会自动加载enableautoconfiguration-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
2.新建resource文件,META-INF/spring.factories文件
多个可这样写:(\表示换行可读取到属性)(,多个属性分割)
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.example.start.demo.service.MyHelloConfiguration,\
- com.example.start.demo.service.MyHelloConfiguration
3.新建MyHelloConfiguration配置类
- @Configuration
public class MyHelloConfiguration {- //当不存在HelloService时加载
- @Bean
- @ConditionalOnMissingBean(HelloService.class)
- public HelloService test() {
- HelloService helloService = new HelloService();
- helloService.setMsg("world");
- return helloService;
- }
- }
- public class HelloService {
- private String msg;
- public void setMsg(String msg) {
- this
- .msg = msg;
- }
- public String sayHello() {
- return "hello" + msg;
- }
- }
4.在IDEA中Maven下执行install将jar包发布到本地仓库
5在项目B中引入项目A新建的spring-boot-starter-demo.jar
- <dependency>
- <groupId>com.example.start</groupId>
- <artifactId>spring-boot-starter-demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
6项目B引用测试结果:
项目B调用项目A的service
启动项目B设置application.properties文件debug=true,启动输出结果:
测试运行结果:
SpringBoot(三)手写starter pom自动配置的更多相关文章
- 利用SpringBoot+Logback手写一个简单的链路追踪
目录 一.实现原理 二.代码实战 三.测试 最近线上排查问题时候,发现请求太多导致日志错综复杂,没办法把用户在一次或多次请求的日志关联在一起,所以就利用SpringBoot+Logback手写了一个简 ...
- SpringBoot自动化配置之四:SpringBoot 之Starter(自动配置)、Command-line runners
Spring Boot Starter是在SpringBoot组件中被提出来的一种概念,stackoverflow上面已经有人概括了这个starter是什么东西,想看完整的回答戳这里 Starter ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- springboot(十四)-分库分表-自动配置
上一节我们是手动配置数据源的,直接在java代码里写数据库的东西,这操作我个人是不喜欢的.我觉得这些东西就应该出现在application.yml文件中. 还有,万一我们的项目在使用之后,突然需要改变 ...
- SpringBoot 系列 - 自己写starter
原文地址: https://www.xncoding.com/2017/07/22/spring/sb-starter.html 前言: Spring Boot由众多Starter组成,随着版本的推移 ...
- Springboot学习:底层依赖与自动配置的原理
springboot依赖的父项目 我们在创建springboot项目的时候,设置了一个父项目: 这个项目可以点进去,可以发现它依赖于另一个父项目 再次点进去,发现没有依赖父项目了 观察这个项目的pom ...
- springboot入门之版本依赖和自动配置原理
前言 Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that ...
- 手写Starter
一. Starter工程的命名 Spring 官方定义的Starter通常命名遵循的格式为spring-boot-starter-{name},例如 spring-boot-starter-web.S ...
- 20、Springboot 与数据访问(JDBC/自动配置)
简介: 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入 各种xxxTemplate,x ...
随机推荐
- luogu_P4767 [IOI2000]邮局
传送门 Description 高速公路旁边有一些村庄.高速公路表示为整数轴,每个村庄的位置用单个整数坐标标识.没有两个在同样地方的村庄.两个位置之间的距离是其整数坐标差的绝对值. 邮局将建在一些,但 ...
- C语言的历史
1.ALGOL语言 ALGOL ,为算法语言(ALGOrithmic Language)的缩写,是计算机发展史上首批产生的高级程式语言家族.当时还是晶体管计算机流行的时代,由于ALGOL语句和普通语言 ...
- 解决vim升级后导致的高亮行行好有下划线问题,
在自己的guodersert.vim中添加下面一行即可 hi CursorLineNr term=bold cterm=NONE ctermfg=darkgreen gui=bold guifg=Ye ...
- beyond compare秘钥被禁
错误提示:This license key has been revoked xxxxx 即: Windows 系统: 解决方法: 删除以下目录中的所有文件即可. C:\Users\Administr ...
- java集合类型源码解析之PriorityQueue
本来第二篇想解析一下LinkedList,不过扫了一下源码后,觉得LinkedList的实现比较简单,没有什么意思,于是移步PriorityQueue. PriorityQueue通过数组实现了一个堆 ...
- linux pthread_cond_signal
pthread_cond_signal函数的作用是发送一个信号给另外一个正在处于阻塞等待状态的线程,使其脱离阻塞状态,继续执行.如果没有线程处在阻塞等待状态,pthread_cond_signal ...
- pgpool-II 高可用搭建
pgpool-II主备流复制的架设1.环境 OS: CentOS release 6.4 (Final)DB: postgresql 9.3.6pgpool服务器: pgpool 172.16.0.2 ...
- 骑行川藏--新都桥&塔公草原
新都桥 塔公草原 新都桥,位于四川省甘孜藏族自治州康定市西部地区,距市区81公里: 别名:东俄罗,一个镇名.海拔约3300米,没有突出的标志性景观,沿线有10余公里被称为“摄影家走廊”. 神奇光线,无 ...
- 【转载】 AutoML技术现状与未来展望
原文地址: https://www.cnblogs.com/marsggbo/p/9309520.html ---------------------------------------------- ...
- DBUtil内部实现过程解读
python数据库连接工具DBUtils DBUtils是一个允许在多线程python应用和数据库之间安全及高效连接的python模块套件. 模块 DBUtils套件包含两个模块子集,一个适用于兼容D ...