SpringBoot应用篇(一):自定义starter
一、码前必备知识
1、SpringBoot starter机制
SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。
2、为什么要自定义starter
在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配,简直不要太爽。
3、自定义starter的案例
以下案例由笔者工作中遇到的部分场景
▲ 动态数据源。
▲ 登录模块。
▲ 基于AOP技术实现日志切面。
。。。。。。
4、自定义starter的命名规则
SpringBoot提供的starter以spring-boot-starter-xxx
的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter
命名规则。以区分SpringBoot生态提供的starter。
5、代码地址
https://github.com/hello-shf/demo-spring-boot-starter.git simple分支哦
二、starter的实现方法
1、新建一个工程
命名为demo-spring-boot-starter
下图为工程目录结构
2、pom依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>0.0.1-RELEASE</version>
<name>demo-spring-boot-starter</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> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
3、定义一个实体类映射配置信息
@ConfigurationProperties(prefix = "demo") 它可以把相同前缀的配置信息通过配置项名称映射成实体类,比如我们这里指定 prefix = "demo" 这样,我们就能将以demo为前缀的配置项拿到了。
ps:其实这个注解很强大,它不但能映射成String或基本类型的变量。还可以映射为List,Map等数据结构。
package com.demo.starter.properties; import org.springframework.boot.context.properties.ConfigurationProperties; /**
* 描述:配置信息 实体
*
* @Author shf
* @Date 2019/5/7 22:08
* @Version V1.0
**/
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
private String sayWhat;
private String toWho; public String getSayWhat() {
return sayWhat;
} public void setSayWhat(String sayWhat) {
this.sayWhat = sayWhat;
} public String getToWho() {
return toWho;
} public void setToWho(String toWho) {
this.toWho = toWho;
}
}
4、定义一个Service
package com.demo.starter.service; /**
* 描述:随便定义一个Service
*
* @Author shf
* @Date 2019/5/7 21:59
* @Version V1.0
**/
public class DemoService {
public String sayWhat;
public String toWho;
public DemoService(String sayWhat, String toWho){
this.sayWhat = sayWhat;
this.toWho = toWho;
}
public String say(){
return this.sayWhat + "! " + toWho;
}
}
5,定义一个配置类
这里,我们将DemoService类定义为一个Bean,交给Ioc容器。
▲ @Configuration 注解就不多说了。
▲ @EnableConfigurationProperties 注解。该注解是用来开启对3步骤中 @ConfigurationProperties 注解配置Bean的支持。也就是@EnableConfigurationProperties注解告诉Spring Boot 能支持@ConfigurationProperties。
当然了,也可以在 @ConfigurationProperties 注解的类上添加 @Configuration 或者 @Component 注解
▲ @ConditionalOnProperty 注解控制 @Configuration 是否生效。简单来说也就是我们可以通过在yml配置文件中控制 @Configuration 注解的配置类是否生效。
package com.demo.starter.config; import com.demo.starter.properties.DemoProperties;
import com.demo.starter.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
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; /**
* 描述:配置类
*
* @Author shf
* @Date 2019/5/7 21:50
* @Version V1.0
**/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
prefix = "demo",
name = "isopen",
havingValue = "true"
)
public class DemoConfig {
@Autowired
private DemoProperties demoProperties; @Bean(name = "demo")
public DemoService demoService(){
return new DemoService(demoProperties.getSayWhat(), demoProperties.getToWho());
}
}
6、最重要的来了
如图,新建META-INF文件夹,然后创建spring.factories文件,
在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置。(笔者努力最近把自动装配的博客写出来)
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.starter.config.DemoConfig
7、测试
在demo-spring-boot-starter工程中执行mvn clean install 一个自定义的starter新鲜出炉。
新建测试工程
引入starter依赖
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>0.0.1-RELEASE</version>
</dependency>
配置文件
demo.isopen=true
demo.say-what=hello
demo.to-who=shf
然后写个测试类。
package com.example.test.controller; import com.demo.starter.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /**
* 描述:
*
* @Author shf
* @Description TODO
* @Date 2019/5/13 15:52
* @Version V1.0
**/
@RestController
public class DemoController {
@Resource(name = "demo")
private DemoService demoService; @GetMapping("/say")
public String sayWhat(){
return demoService.say();
} }
浏览器
SpringBoot应用篇(一):自定义starter的更多相关文章
- SpringBoot第十六篇:自定义starter
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 这一段时间 ...
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
- SpringBoot Starter机制 - 自定义Starter
目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言 最近在学习Sp ...
- SpringBoot自定义starter开发分布式任务调度实践
概述 需求 在前面的博客<Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战>中,我们已经熟悉ElasticJob分布式任务的应用,其核心实现为elasticjob- ...
- java框架之SpringBoot(10)-启动流程及自定义starter
启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...
- (springboot)自定义Starter
要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...
- SpringBoot自定义starter及自动配置
SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...
- SpringBoot系列教程web篇之自定义异常处理HandlerExceptionResolver
关于Web应用的全局异常处理,上一篇介绍了ControllerAdvice结合@ExceptionHandler的方式来实现web应用的全局异常管理: 本篇博文则带来另外一种并不常见的使用方式,通过实 ...
- SpringBoot自定义Starter实现
自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...
随机推荐
- Java -- 内部类, 成员内部类,局部内部类,匿名内部类,闭包和回调, 枚举类
1. 成员内部类分为 静态内部类 和 非静态内部类. 非静态内部类 和 外部类的其他成员一样处理, 非静态内部类可以访问外部类的private的属性,而外部类不能访问非静态内部类的属性,需要实例非静 ...
- Spark- Checkpoint原理剖析
Checkpoint,是Spark 提供的一个比较高级的功能.有的时候,比如说,我们的 Spark 应用程序,特别的复杂,然后从初始的RDD开始,到最后拯个应用程序完成,有非常多的步骤,比如超过20个 ...
- 七 Django框架,models.py模块,数据库操作——F和Q()运算符:|或者、&并且——queryset对象序列化
F()可以将数据库里的数字类型的数据,转换为可以数字类型 首先要导入 from django.db.models import F from django.shortcuts import rende ...
- 趣味Shell
Richard M. Stallman大神是谁就不用说了,一时来了兴趣,想看看Linux系统下有多少程序有这位大神参与编写的. 先把所有命令导出到文件中,遍历所有命令,用man手册查一下并过滤Stal ...
- error: ‘errno’ was not declared in this scope
问题: 将一个c文件改为cpp文件,其中的perror()改用C++中的std::cerr << strerror(error) << std::endl;来替换. 重新编译文 ...
- loj515贪心只能过样例
bitset练习题... 位运算真的是玄学... 一开始真的“只能过样例” 后来发现把左移写成了小于号 鬼知道我在想什么/手动微笑 loj第一题 #include<iostream> #i ...
- Django来敲门~第一部分【4. 创建第一个模块应用】
成若缺,其用不弊.大盈若冲,其用不穷.大直若屈.大巧若拙.大辩若讷.静胜躁,寒胜热.清静为天下正 ——老子<道德经> 本章内容 创建应用(app) 开发第一个视图(View) URL访问配 ...
- 迁移学习-微调(fine-tune)的注意事项:
选取微调形式的两个重要因素:新数据集的大小(size)和相似性(与预训练的数据集相比).牢记卷积网络在提取特征时,前面的层所提取的更具一般性,后面的层更加具体,更倾向于原始的数据集(more orig ...
- Maven: 自动远程部署
1. 在settings.xml中的Servers节点中增加Server的登录信息: <server> <id>deploy_server_65</id> < ...
- node.js的国内源
node.js在使用npm安装包是,由于源是国外的,有可能会被GFW屏蔽. 通过下面的方法可以把源指向国内的. 具体方法如下: 编辑 ~/.npmrc 加入下面内容 registry = http:/ ...