在我们学习 SpringBoot 时都已经了解到 starterSpringBoot 的核心组成部分, SpringBoot 为我们提供了尽可能完善的封装,提供了一系列的自动化配置的 starter 插件,我们在使用 spring-boot-starter-web 时只需要在 pom.xml 配置文件内添加依赖就可以了,我们之前传统方式则是需要添加很多相关 SpringMVC 配置文件。而 spring-boot-starter-web 为我们提供了几乎所有的默认配置,很好的降低了使用框架时的复杂度。

因此在使用xx.starter时你就不用考虑该怎么配置,即便是有一些必要的配置在application.properties配置文件内对应配置就可以了,那好,为什么我在application.properties配置对应属性后xx.starter就可以获取到并作出处理呢?下面我们带着这个疑问来编写我们自定义的starter让我们深入了解SpringBoot

本章目标

自定义starter并且通过spring-boot-autoconfigure完成自动化配置。

构建项目

创建starter项目我们并不需要创建SpringBoot项目,我们创建一个Maven项目就可以满足我们的需求,创建项目完成后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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yuqiyu</groupId><artifactId>chapter28</artifactId><version>1.0.0</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>1.5.4.RELEASE</version></dependency></dependencies>
</project>

我们这个starter并不做其他复杂逻辑的编写,所以这里的依赖只是添加了spring-boot-autoconfigure,实战开发时可以添加任意依赖到项目中。

配置映射参数实体

我们在文章开头埋下了一个疑问,starter是如何读取application.properties或者application.yml配置文件内需要的配置参数的呢?那么接下来我们就看看如何可以获取自定义的配置信息。
SpringBoot在处理这种事情上早就已经考虑到了,所以提供了一个注解@ConfigurationProperties,该注解可以完成将application.properties配置文件内的有规则的配置参数映射到实体内的field内,不过需要提供setter方法,自定义配置参数实体代码如下所示:

package com.bbaiggey.cloud;
import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "hello")
public class HelloProperties
{//消息内容private String msg = "hello";//是否显示消息内容private boolean show = true;public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public boolean isShow() {return show;}public void setShow(boolean show) {this.show = show;}
}

在上面代码中,@ConfigurationProperties注解内我们使用到了属性preffix,该属性配置了读取参数的前缀,根据上面的实体属性对应配置文件内的配置则是hello.msghello.show,当然我们提供了默认值,配置文件内不进行配置时则是使用默认值。

编写自定义业务

我们为自定义starter提供一个Service,并且提供一个名为sayHello的方法用于返回我们配置的msg内容。代码如下所示:

package com.bbaiggey.cloud;public class HelloService
{//消息内容private String msg;//是否显示消息内容private boolean show = true;public String sayHello(){return show ? "Hello," + msg : "disable";}public void setMsg(String msg) {this.msg = msg;}public void setShow(boolean show) {this.show = show;}
}

我们Service内的代码比较简单,根据属性参数进行返回格式化后的字符串。

接下来我们开始编写自动配置,这一块是starter的核心部分,配置该部分后在启动项目时才会自动加载配置,当然其中有很多细节性质的配置

实现自动化配置

自动化配置其实只是提供实体bean的验证以及初始化,我们先来看看代码:

package com.bbaiggey.cloud;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration//开启配置
@EnableConfigurationProperties(HelloProperties.class)//开启使用映射实体对象
@ConditionalOnClass(HelloService.class)//存在HelloService时初始化该配置类
@ConditionalOnProperty//存在对应配置信息时初始化该配置类(prefix = "hello",//存在配置前缀hellovalue = "enabled",//开启matchIfMissing = true//缺失检查)
public class HelloAutoConfiguration
{//application.properties配置文件映射前缀实体对象@Autowiredprivate HelloProperties helloProperties;/*** 根据条件判断不存在HelloService时初始化新bean到SpringIoc* @return*/@Bean//创建HelloService实体bean@ConditionalOnMissingBean(HelloService.class)//缺失HelloService实体bean时,初始化HelloService并添加到SpringIocpublic HelloService helloService(){System.out.println(">>>The HelloService Not Found,Execute Create New Bean.");HelloService helloService = new HelloService();helloService.setMsg(helloProperties.getMsg());//设置消息内容helloService.setShow(helloProperties.isShow());//设置是否显示return helloService;}
}

自动化配置代码中有很多我们之前没有用到的注解配置,我们从上开始讲解

@Configuration:这个配置就不用多做解释了,我们一直在使用
@EnableConfigurationProperties:这是一个开启使用配置参数的注解,value值就是我们配置实体参数映射的ClassType,将配置实体作为配置来源。

SpringBoot内置条件注解

有关@ConditionalOnXxx相关的注解这里要系统的说下,因为这个是我们配置的关键,根据名称我们可以理解为具有Xxx条件,当然它实际的意义也是如此,条件注解是一个系列,下面我们详细做出解释

@ConditionalOnBean:当SpringIoc容器内存在指定Bean的条件
@ConditionalOnClass:当SpringIoc容器内存在指定Class的条件
@ConditionalOnExpression:基于SpEL表达式作为判断条件
@ConditionalOnJava:基于JVM版本作为判断条件
@ConditionalOnJndi:在JNDI存在时查找指定的位置
@ConditionalOnMissingBean:当SpringIoc容器内不存在指定Bean的条件
@ConditionalOnMissingClass:当SpringIoc容器内不存在指定Class的条件
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件
@ConditionalOnProperty:指定的属性是否有指定的值
@ConditionalOnResource:类路径是否有指定的值
@ConditionalOnSingleCandidate:当指定BeanSpringIoc容器内只有一个,或者虽然有多个但是指定首选的Bean
@ConditionalOnWebApplication:当前项目是Web项目的条件

以上注解都是元注解@Conditional演变而来的,根据不用的条件对应创建以上的具体条件注解。

到目前为止我们还没有完成自动化配置starter,我们需要了解SpringBoot运作原理后才可以完成后续编码。

Starter自动化运作原理

在注解@SpringBootApplication上存在一个开启自动化配置的注解@EnableAutoConfiguration来完成自动化配置,注解源码如下所示:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//package org.springframework.boot.autoconfigure;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";Class<?>[] exclude() default {};String[] excludeName() default {};
}

@EnableAutoConfiguration注解内使用到了@import注解来完成导入配置的功能,而EnableAutoConfigurationImportSelector内部则是使用了SpringFactoriesLoader.loadFactoryNames方法进行扫描具有META-INF/spring.factories文件的jar包。我们可以先来看下spring-boot-autoconfigure包内的spring.factories文件内容,如下所示:

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnClassCondition

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
.....省略

可以看到配置的结构形式是Key=>Value形式,多个Value时使用,隔开,那我们在自定义starter内也可以使用这种形式来完成,我们的目的是为了完成自动化配置,所以我们这里Key则是需要使用org.springframework.boot.autoconfigure.EnableAutoConfiguration

自定义spring.factories

我们在src/main/resource目录下创建META-INF目录,并在目录内添加文件spring.factories,具体内容如下所示:

#配置自定义Starter的自动化配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bbaiggey.cloud.HelloAutoConfiguration

都目前为止我们的自定义starter已经配置完成,下面我们需要新建一个SpringBoot项目来测试我们的自动化配置是否已经生效。

创建测试SpringBoot项目

在使用自定义starter之前需要将starterMaven Jar Install到本地,我们使用idea工具自带的maven命令完成该操作

步骤:工具右侧 -> Maven Projects -> Lifecycle -> install

创建测试项目的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yuqiyu.sample</groupId><artifactId>test-spring-boot-starter-hello</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>test-spring-boot-starter-hello</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>                <!--自定义starter依赖--><dependency><groupId>com.yuqiyu</groupId><artifactId>chapter28</artifactId><version>1.0.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

我们只需要将依赖添加到pom.xml配置文件内

运行测试

在运行项目之前,我们打开application.properties配置文件开启debug模式,查看自动化配置的输出日志,配置内容如下所示:

#显示debug日志信息
debug=true

接下来我们启动项目,在控制台查找是否存在我们的HelloAutoConfiguration日志输出,控制台输出内容如下所示:

.....省略
>>>The HelloService Not Found,Execute Create New Bean.
.....省略HelloAutoConfiguration matched:- @ConditionalOnClass found required class 'com.bbaiggey.cloud.HelloService'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnProperty (hello.enabled) matched (OnPropertyCondition)
HelloAutoConfiguration#helloService matched:- @ConditionalOnMissingBean (types: com.bbaiggey.cloud.HelloService; SearchStrategy: all) did not find any beans (OnBeanCondition)
.....省略

在控制台可以看到我们的自定义starter的自动化配置已经生效了,并且根据@ConditionalOnMissingBean(HelloService.class)做出了条件注入HelloService实体bean到SpringIoc容器内

编写测试控制器

我们来编写一个简单的测试控制器,查看HelloService在不配置参数情况下输出格式化字符串内容,控制器代码如下所示:

package com.bbaiggey.cloud.sample.testspringbootstarterhello;import com.bbaiggey.cloud.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController
{//注入自定义starter内逻辑@AutowiredHelloService helloService;/*** 测试访问地址/hello* @return 格式化字符串*/@RequestMapping(value = "/hello")public String sayHello(){return helloService.sayHello();}
}

接下来我们重启下项目,访问地址http://127.0.0.1:8080/hello,界面输出内容如下所示:

Hello,bbaiggey

界面输出的内容是我们默认值,接下来我们在application.properties配置文件内对应添加hello.msghello.show配置参数,如下所示:

#配置自定义starter参数
hello.msg=bbaiggeyhello.show=true

重启项目,再次访问地址,界面输出内容如下所示:

Hello,bbaiggey

我们的配置生效了,到目前为止我相信大家已经明白了我们application.properties配置文件为什么可以作为统一配置入口,为什么配置后可以被对应starter所使用。

总结

以上内容是本章的全部讲解,本章主要讲解了我们如何自定义starter并且自动化配置到SpringBoot项目中,当然里面还有很多神奇的地方需要大家去深入挖掘。

SpringBoot使用AutoConfiguration自定义Starter相关推荐

  1. 根据自动装配原理在Springboot项目中自定义starter,并实现热插拔技术,自定义@enable

    根据自动装配原理在Springboot项目中自定义starter,并实现热插拔技术 自定义starter 简单步骤 使用测试 优化(热插拔技术) 自定义starter 简单步骤 创建项目,并引入aut ...

  2. springboot框架学习 - 自定义 starter

    本篇主要演示 springboot 中自定义 starter 的实例,不涉及底层原理. 首先,创建一个什么都没有的工程作为父工程,这个工程不需要进行任何操作,然后创建两个模块,分别命名为 tyu-he ...

  3. SpringBoot自定义Starter(自动配置类)

    前言 SpringBoot其实从诞生以来围绕的核心就是快速构建项目,快速构建的前提是有人帮你做好轮子,开发者只要拿来即用就好了,而造好轮子的人就是SpringBoot的开发者,引入自动配置的形式帮助开 ...

  4. @configurationproperties注解的使用_徒手使用SpringBoot自定义Starter启动器

    前言 在使用SpringBoot框架时,SpringBoot 最强大的功能就是把我们常用的场景抽取封装成了一个个starter,将其称之为场景启动器.搭建项目时,通过引入SpringBoot为我提供的 ...

  5. 玩转 SpringBoot 2.x 之自定义Starter依赖

    前言 SpringBoot 核心功能之一就是 starter 依赖也叫起步依赖,他默认提供了 spring-boot-starter-web.spring-boot-starter-logging.s ...

  6. SpringBoot项目为什么需要引入大量的starter?如何自定义starter

    为什么我们在使用SpringBoot框架开发Java Web应用需要引入大量的starter?例如,我们引入Redis就在Maven中导入 spring-boot-starter-data-redis ...

  7. SpringBoot 自定义Starter(阿里云短信、消息推送)

    首先在IDEA中创建SpringBoot项目,引入相关必要依赖,本次以阿里云短信/消息推送为例: <dependency><groupId>com.aliyun</gro ...

  8. SpringBoot - 开发自定义starter

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

  9. spring boot @value取不到值_看看大厂如何自定义starter,还真想不到

    我自己整理了一套java架构资料,还有大厂面试题,需要私信回复"资料"就好了,无偿分享,希望对你有帮助. 背景 使用过Spring Boot的小伙伴都应该知道,一个Spring B ...

  10. 【读官方文档,学原味技术】SpringBoot-Staters和自定义Starter

    spring-boot-reference 如果不想阅读英文原文,你可以直接读本文的[TS]标注参考翻译内容.由于本人水平有限,如有理解错误,烦请指正,互相交流. Lire les documents ...

最新文章

  1. Python数据收集及分析学习相关
  2. 计算机听不到音乐怎么回事,Win10电脑设置麦克风提示“计算机听不到任何声音”如何解决...
  3. jzoj3461-小麦亩产一千八【斐波那契数列】
  4. java(线程特性,Volatile作用,java内存模型)
  5. C语言实现随机发纸牌
  6. 如何将java程序打包成exe文件?
  7. Windows下Apache Tomcat 8安装配置
  8. Jenkins自动化UI测试Tomcat报错“The web application [/jenkins] appears to have started a thread named”解决办法...
  9. 安卓一键清理内存_教大家安卓怎么清理内存-装修攻略
  10. 太网交换机芯片-KSZ系列
  11. 基于opengl编写简单的二维绘图软件
  12. 吴军三部曲见识(三) 谈谈见识
  13. php如何计算天数,php计算日期相差天数二种方法
  14. 【windows 壁纸】
  15. 深入浅出matplotlib(42):动画显示与MP4视频输出五
  16. 腾讯互娱技术总监张正:《天涯明月刀》后台技术创新
  17. cmd命令怎么查看电脑配置?
  18. 计算机文档里的圆圈,请问在WORD文档里怎样插入数字上的圆点?
  19. MacBook如何设置一键锁屏或者快捷锁屏?
  20. 古老的视频去噪算法(FLT_GradualNoise)解析并优化,可实现1920*1080 YUV数据400fps的处理能力

热门文章

  1. 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_04.mybatis概述
  2. 【HDU5409】CRB and Graph 边双联通 子树最值
  3. Python程序设计--第5章 函数设计与使用
  4. 2019-05-16mysql忘记密码怎么办
  5. vs2015未能正确加载“ProviderPackage”包
  6. 利用 Commons-Fileupload 实现文件上传分析(转)
  7. 旧板与IO板之间的连接
  8. 在SharePoint 2010中创建联系人Web数据库网站
  9. ES6的新特性(9)——对象的扩展
  10. cookie和session基础以及在Django中应用