08_springboot2.x自定义starter
概述
starter:启动器
1、这个场景需要使用到的依赖是什么?
2、如何编写自动配置
规则:
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
模式:
启动器只用来做依赖导入;
专门来写一个自动配置模块;
启动器依赖自动配置;别人只需要引入启动器(starter)
mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter
启动器(starter)
–启动器模块是一个空JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库
命名规约:
•推荐使用以下命名规约
官方命名空间
–前缀:“spring-boot-starter-”
–模式:spring-boot-starter-模块名
–举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
自定义命名空间
–后缀:“-spring-boot-starter”
–模式:模块-spring-boot-starter
–举例:mybatis-spring-boot-starter
图解

1、编写启动器XXX-starter

2、自动配置模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spboot</groupId>
<artifactId>jatpeo-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jatpeo-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
应用举例:
HelloProperties 配置文件
package com.spboot.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "jatpeo.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
HelloService
package com.spboot.starter;
public class HelloService {
HelloProperties helloProperties;
public String sayHello(String name){
return helloProperties.getPrefix()+ name + helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
自动配置类:
HelloServiceAutoConfiguration
package com.spboot.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication//web应用起效果
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
注意:这里让自动配置类生效得在META-INF/新建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.spboot.starter.HelloServiceAutoConfiguration
分别安装到maven仓库

注意安装顺序
测试:
新建项目
pom.xml

注意:引用的是我们配置的启动器的
application.properties

controller:
package com.spboot.springboot.controller;
import com.spboot.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class helloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String say(){
return helloService.sayHello("dyn");
}
}
浏览器测试:

08_springboot2.x自定义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自身的结构 ...
随机推荐
- leetcood学习笔记-226- 翻转二叉树
题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: Tree ...
- R语言 环境设置
尝试在线环境 你真的不需要设置自己的环境来开始学习R编程语言. 原因很简单,我们已经在线设置了R编程环境,以便您可以在进行理论工作的同时在线编译和执行所有可用的示例. 这给你对你正在阅读的信心,并用不 ...
- 干货满满!解密阿里云RPA (机器人流程自动化)的产品架构和商业化发展
阿里云RPA,作为阿里云自研8年的技术,在资本的热捧下,逐渐从幕后来到台前,成为企业服务市场的黑马.本文将从产品上全面剖析,阿里云RPA这款产品的现阶段情况,同时简单谈谈阿里云RPA的商业化进展. 阿 ...
- thinkphp 模板驱动
模板引擎驱动完成了第三方模板引擎的支持,通过定义模板引擎驱动,我们可以支持Smarty.TemplateLite.SmartTemplate和EaseTemplate等第三方模板引擎. 默认的模板引擎 ...
- NX二次开发-NXOpen中Point3d类型转换成point类型
NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...
- git删除持久化存储的账号密码.git-credentials
git在提交时,发现提交不了,个人信息不是自己的,通过 git config --list 看到邮箱和用户名都是自己的. 这时候就需要把以前记录过的信息都删掉. 1. 通过 git config -- ...
- [JZOJ 5600] Arg
题意:求最少LIS覆盖... 思路: 计算\(LIS\)时我们一般用\(dp\)表示到当先位置时以当前位置结尾的\(LIS\)最长长度. 那么这个数组保证单调不降,我们考虑二进制表示. 然后就是转移了 ...
- P1624 单词缩写
P1624 单词缩写 题目描述 树树发现好多计算机中的单词都是缩写,如GDB是全称Gnu DeBug的缩写.但是,有时候缩写对应的全称会不固定,如缩写LINUX可以理解为: (1) LINus’s U ...
- 如何再windows里面装Tableau Server
1.进入网页http://www.tableau.com/support/esdalt下载所需要的软件. 我的是正版下载的是 2.点击tableauServer-64bit-10....这个软件 一直 ...
- Delphi中的Sender:TObject对象解析转载
https://blog.csdn.net/jl_tiny/article/details/24376661 Delphi中的Sender:TObject对象解析 procedure TForm1.B ...