前言

  在之前的博文(Spring Boot自动配置原理与实践(一))中,已经介绍了Spring boot的自动配置的相关原理与概念,本篇主要是对自动配置的实践,即自定义Starter,对原理与概念加深理解。

  本篇是我在实际工作中配置的用于弱口令检查的Starter,能方便嵌入到用户模块中的相关密码接口或方法,对弱口令进行检查并反馈,当然由于是公司内部代码,部分代码省略。


一、Starter实践

1、配置Maven依赖

Spring Boot自动化配置主要依赖如下两个包:

  • spring-boot-starter:打包starter主要依赖
  • configuration-processor:自动化配置主要依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

2、创建实体类映射配置信息

众所周知,SpringBoot Starter最厉害的就是可以通过最简单的properties/yaml文件配置,达到最终目的。配置文件需要通过解析生成对应的实体类

@ConfigurationProperties(prefix = "weak.password")
public class CheckWeakPasswordProperties { private Boolean enabled = true;
/**
* 需要检查的URI数组
*/
private String[] checkUri;
/**
* 拦截检查的方式 1-interceptor 2-filter 3-aop
*/
private Integer checkType = 1;
private String ip = "127.0.0.1";
private String port = "8501";
/**
* 客户端名称
*/
private String clientName = "cloud-user";
/**
* 校验失败信息提示
*/
private String failureMessage = "密码等级不够";
  
   ...// 省略getter/setter方法

其中prefix = "weak.password",标明配置文件以“weak.password”开头的字段(对应实体类中的字段)都是需要解析的。在配置文件中输入前缀后,会进行提示说明

3、定义配置类

这一步非常关键,是自动装配的核心,通过配置文件配置灵活的参数产生相关的Bean,完成一系列初始化操作,关键的几个注解在这里就不解释了,具体可以看Spring Boot自动配置原理与实践(一)

@Configuration
@EnableConfigurationProperties(CheckWeakPasswordProperties.class)
@ConditionalOnProperty(prefix = "weak.password", name = "enabled", havingValue = "true")
public class CheckWeakPasswordAutoConfiguration { public CheckWeakPasswordAutoConfiguration() {
} @Bean
@ConditionalOnProperty(prefix = "weak.password", name = "checkType", havingValue = "2")
public CheckPasswordInterceptor checkPasswordInterceptor(){
return new CheckPasswordInterceptor();
}
@Bean
@ConditionalOnProperty(prefix = "weak.password", name = "checkType", havingValue = "2")
public CheckPasswordFilter checkPasswordFilter(){
return new CheckPasswordFilter();
}
@Bean
@ConditionalOnProperty(name = "weak.password.check-type", havingValue = "2")
public CheckPasswordFilterConfig checkPasswordFilterConfig() {
return new CheckPasswordFilterConfig();
}
@Bean
@ConditionalOnProperty(prefix = "weak.password", name = "checkType", havingValue = "1")
public CheckPasswordInterceptorConfig checkPasswordInterceptorConfig(){
return new CheckPasswordInterceptorConfig();
} }

4、创建spring.factories文件

 之前三步所有的操作都已经完成,那么将Starter当引入工程中是如何发现并自动装配的,这就需要spring.factory文件中标明,在resource/META-INF在新建spring.factory文件

在该文件中指明AutoConfiguration的全Class路径

这样打包的时候就能将spring.factory文件打包,项目启动的时候就会扫描并装配

同时生成spring-configuration-metadata.json文件,其内容就是提供配置文件智能化提示的

{
"groups": [
{
"name": "weak.password",
"type": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties"
}
],
"properties": [
{
"name": "weak.password.check-type",
"type": "java.lang.Integer",
"description": "拦截检查的方式 1-interceptor 2-filter 3-aop",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"defaultValue": 1
},
{
"name": "weak.password.check-uri",
"type": "java.lang.String[]",
"description": "需要检查的URI数组",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties"
},
{
"name": "weak.password.client-name",
"type": "java.lang.String",
"description": "客户端名称",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"defaultValue": "cloud-user"
},
{
"name": "weak.password.enabled",
"type": "java.lang.Boolean",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"defaultValue": true
},
{
"name": "weak.password.failure-message",
"type": "java.lang.String",
"description": "校验失败信息提示",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"defaultValue": "密码等级不够"
},
{
"name": "weak.password.ip",
"type": "java.lang.String",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"defaultValue": "127.0.0.1"
},
{
"name": "weak.password.port",
"type": "java.lang.String",
"sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties",
"defaultValue": "8501"
}
],
"hints": []
}

二、自定义Starter使用

首先引入自定义的Starter包依赖到相关应用中

然后在配置文件中打开开关,或者某些条件才能开启自动配置,以我的代码示例举例的话,就是需要指定enabled为true

其次可以观察启动的时候相关的Bean是否被自动装配,可以打开debug模式查看日志,或者在idea中查看Endpoints-->Beans-->application,可以看到相关的自动配置启动时加载了,并且相应的Bean也注入了。

最后就是验证是否符合业务逻辑

Spring Boot自动配置原理与实践(二)的更多相关文章

  1. Spring Boot自动配置原理与实践(一)

    前言 Spring Boot众所周知是为了简化Spring的配置,省去XML的复杂化配置(虽然Spring官方推荐也使用Java配置)采用Java+Annotation方式配置.如下几个问题是我刚开始 ...

  2. Spring Boot自动配置原理(转)

    第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot- ...

  3. Spring Boot自动配置原理、实战

    Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...

  4. Springboot 系列(三)Spring Boot 自动配置原理

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...

  5. Spring Boot 自动配置原理(精髓)

    一.自动配置原理(掌握) SpringBoot启动项目会加载主配置类@SpringBootApplication,开启@EnableAutoConfiguration自动配置功能 @EnableAut ...

  6. spring boot 自动配置原理

    1).spring boot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration,先看一下启动类的main方法 public ConfigurableApplic ...

  7. Spring Boot自动配置原理

    使用Spring Boot之后,一个整合了SpringMVC的WEB工程开发,变的无比简单,那些繁杂的配置都消失不见了,这 是如何做到的? 一切魔力的开始,都是从我们的main函数来的,所以我们再次来 ...

  8. 【串线篇】spring boot自动配置原理

    配置文件到底能写什么?怎么写?自动配置原理: 配置文件能配置的属性参照 一.自动配置原理: 1.1.SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfigur ...

  9. Spring Boot 自动配置原理是什么?

    注解 @EnableAutoConfiguration, @Configuration, @ConditionalOnClass 就是自动配置的核心,首先它得是一个配置文件,其次根据类路径下是否有这个 ...

随机推荐

  1. C#使用FtpWebRequest 基础连接已经关闭:连接被意外关闭(The underlying connection was closed:The connection was closed unexpectedly)

    公司内部开发的winform程序使用了FtpWebRequest下载FTP服务器的文件到本地. 大多数人运行良好,由于我们是试运行逐步有人加入到平台的使用,前两天突然有个别机器无法连接FTP服务器报出 ...

  2. shiro框架整合ssm框架

    下面我通过一个web的maven项目来讲解如何将shiro整合ssm框架,具体结构如下图 一.引入依赖的jar包 <?xml version="1.0" encoding=& ...

  3. (转) PHP实现从1累加到100(1+2+….+100=)的几种思路,挺有意思的!!!

    一个经典的小学问题也是一个简单的PHP小应用,1+2+3--100=多少?使用PHP应该怎么写? 这里总结了以下几种思路: 1.普通PHPer: $sum=0;for($i=1;$i<=100; ...

  4. CG-CTF Our 16bit Games

    一.放到xp上面跑,发现是一个图形界面的飞机游戏...估计是分数到达多少,然后就可以输出flag. 打开ida,一脸懵逼,主要这玩意16位,我直接静态调试了 发现很多汇编代码,有点懵逼,在最下方的地方 ...

  5. 机器学习Sklearn系列:(三)决策树

    决策树 熵的定义 如果一个随机变量X的可能取值为X={x1,x2,..,xk},其概率分布为P(X=x)=pi(i=1,2,...,n),则随机变量X的熵定义为\(H(x) = -\sum{p(x)l ...

  6. 入门Kubernetes-minikube本地k8s环境

    前言: 在上一篇 结尾中使用到了minikube方式来做k8s本地环境来学习k8s. 那么这篇先了解下minikube及使用 一.Minikube 简介 minikube 在 macOS.Linux ...

  7. 备战-Java 并发

    备战-Java 并发 谁念西风独自凉,萧萧黄叶闭疏窗 简介:备战-Java 并发. 一.线程的使用 有三种使用线程的方法: 实现 Runnable 接口: 实现 Callable 接口: 继承 Thr ...

  8. [刘阳Java]_第一个Java程序_第7讲

    1. 其实第一个Java程序是很简单,但是当自己编写第一个Java程序时候需要注意如下几个内容: 理解Java程序的运行环境 校验你的Java环境变量是否能够运行你所写的第一个Java程序 理解Jav ...

  9. HTML - form表单操作

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. 架构之:REST和HATEOAS

    目录 简介 HATEOAS简介 HATEOAS的格式 HATEOAS的Spring支持 总结 简介 我们知道REST是一种架构方式,它只是指定了六种需要遵循的基本原则,但是它指定的原则都比较宽泛,我们 ...