Spring Boot自动配置原理与实践(二)
前言
在之前的博文(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也注入了。
最后就是验证是否符合业务逻辑
原文地址:https://www.cnblogs.com/jian0110/p/15152884.html
- React项目配置5(引入MockJs,实现假接口开发)
- POj 1611 The Suspects
- React项目配置4(如何在开发时跨域获取api请求)
- Laravel-博客实战+踩坑laravel-blog最终的效果踩的坑
- React项目配置3(如何管理项目API接口)
- React第三方组件3(状态管理之Flux的使用④TodoList下)
- React第三方组件3(状态管理之Flux的使用③TodoList中)
- Vue实现百度下拉提示搜索一、前期准备二、代码实现三、实现效果
- React第三方组件3(状态管理之Flux的使用②TodoList上)
- ggplot2玫瑰图案例——星巴克门店分布图
- React第三方组件3(状态管理之Flux的使用①简单使用)
- 用ggplot2画了一个我也叫不上名的炫酷图表
- React技巧8(不再手动绑定this,跟.bind(this)说88)
- 美美的商务范儿——ggplot2蝴蝶图
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- codeforces 544C(完全背包求方案数)
- SCU2511(单调栈)
- B. Ternary String(贪心)
- codeforces 940B(贪心)
- codeforces 429A(dfs)
- codeforces 1182B (DFS)
- codeforces 509B(构造,思维)
- codeforces 1257C(map)
- codeforces 977D(DFS)
- codeforces 580C(dfs)
- codeforces 1155B(博弈)
- XXXXX codeforces1364A(数学)
- JAVA入门学习十一
- codeforces 1282B(dp)
- 超级书架2(dfs)