概述

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的更多相关文章

  1. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

  2. Spring Boot 自定义 starter

    一.简介 SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应 ...

  3. java框架之SpringBoot(10)-启动流程及自定义starter

    启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...

  4. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  5. SpringBoot第十六篇:自定义starter

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   这一段时间 ...

  6. 小代学Spring Boot之自定义Starter

    想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...

  7. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  8. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  9. 对照谈-官方spring-boot-starter和自定义starter异同分析

    在前面我讲用spring-boot-starter-mail发邮件的时候,我侧重看的是spring boot发邮件的便利性,今天,我们聊下另外一个方面,spring-boot-starter自身的结构 ...

随机推荐

  1. 【Nginx】Nginx配置

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  2. HTML5布局篇

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  3. thinkphp 入口绑定

    入口绑定是指在应用的入口文件中绑定某个模块,甚至还可以绑定某个控制器和操作,用来简化URL地址的访问. 绑定模块 例如,我们定义了一个入口文件admin.php,希望可以直接访问Admin模块,那么我 ...

  4. 【Codeforces Round #589 (Div. 2) D】Complete Tripartite

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...

  5. TopCoder[TCO2016 Round 1A]:EllysTree(1000)

    Problem Statement      Elly has a graph with N+1 vertices, conveniently numbered from 0 to N. The gr ...

  6. 依赖背包优化——hdu1561

    傻逼依赖背包的优化 #include<bits/stdc++.h> using namespace std; #define N 205 ]; int head[N],tot,n,m,a[ ...

  7. [Android开发常见问题-4] RunTime.exec()如何以root权限执行多条指令?

    转:http://blog.csdn.net/alexander_xfl/article/details/9150971 RunTime.exec()这个接口可以说是给我们开发者提供了一个很好的直观操 ...

  8. hexo next主题深度优化(二),懒加载。

    文章目录 tip:没有耐心的可以直接看:正式在hexo next中加入懒加载(最下面) 废话 背景 懒加载简单介绍 引入js 重点!敲黑板了!!! 完善懒加载函数 懒加载函数可配置的参数 正式在hex ...

  9. 如何查看jdk版本和路径

    cmd进入命令提示符,查看jdk版本,输入java -version;查看jdk路径 ,输入set java home.,这个也是默认路径

  10. Error creating bean with name 'unMblTotController': 注入失败

    今天新来的小伙子,进公司做项目,然后自己新建了包,出了以下错误 y.UnsatisfiedDependencyException: Error creating bean with name 'unM ...