排版设计英文,做网站分为竞价和优化,最新传奇手游开服网站,wordpress 短信登录密码错误1. 概述在项目实际开发过程中#xff0c;为了更好的复用#xff0c;我们参考Spring Boot Starters#xff0c;封装了许多企业内部中间件的starter。这些中间件的接入都需要申请并在项目中配置一些特定的参数。我们通过ConfigurationProperties注解#xff0c;增加了在配置过…1. 概述在项目实际开发过程中为了更好的复用我们参考Spring Boot Starters封装了许多企业内部中间件的starter。这些中间件的接入都需要申请并在项目中配置一些特定的参数。我们通过ConfigurationProperties注解增加了在配置过程的体验实现属性提示。一些参数是在中间件使用过程中的必选参数经常发现项目启动了调用中间件时抛出异常提示某个参数没有配置。这样的接入过程体验很不好。有没有一种方式在项目启动的时候就对配置的参数直接进行校验而不是等到实际使用的时候再抛出错误提示呢很幸运Spring已经为我们提供的Java Validation可以解决我们的问题。2. 增加启动校验在我们创建的Properties类中增加Validation相关配置/** * ConfigProperties. * * author Wang Jianchao(tinyking) * * Created on 2020/8/20 */ValidatedDataConfigurationProperties(prefix config)public class ConfigProperties { NotEmpty private String name;}Validated是Spring提供的校验注解通过该注解告诉Spring这个类是需要进行校验处理的。NotEmpty 是Java Validation API中提供的校验注解表示name字段不能为空。不能为空是指不能是null也不能是空字符串。配置BeanConfigurationEnableConfigurationProperties(ConfigProperties.class)public class ValidationConfiguration { Bean public ConfigProperties configProperties() { return new ConfigProperties(); }}上面的配置就会校验我们在application.yml中有没有配置config.name参数。如果在配置文件中没有该配置项目启动就会失败并抛出校验异常***************************APPLICATION FAILED TO START***************************Description:Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under config to io.github.tinyking.springvalidation.config.ConfigProperties failed: Property: config.name Value: null Reason: 不能为空在application.yml中我们增加config.name等配置项但是不给它设置内容config:name: 再次启动项目发现错误信息发生了变化***************************APPLICATION FAILED TO START***************************Description:Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under config to io.github.tinyking.springvalidation.config.ConfigProperties failed: Property: config.name Value: Origin: class path resource [application.yml]:2:8 Reason: 不能为空Action:Update your applications configuration3. 自定义校验Spring还支持我们进行自定义校验规则通过自定义校验可以实现复杂的校验逻辑。我们创建一个校验类并实现Validator接口/** * ConfigPropertiesValidator. * * author Wang Jianchao(tinyking) * * Created on 2020/8/20 */public class ConfigPropertiesValidator implements Validator { Override public boolean supports(Class aClass) { return ConfigProperties.class.isAssignableFrom(aClass); } Override public void validate(Object target, Errors errors) { ConfigProperties config (ConfigProperties) target; if (!StringUtils.isEmpty(config.getName()) config.getName().length() 需要注意的是上面的Validator接口是Spring框架提供的不是Java Validation API中的接口。supports(Class aClass)方法用来判断是否支持当前正在进行校验的实例validate(Object target, Errors errors)方法中写实际的校验规则并用errors收集错误信息接下来就是如何在Spring Boot启动时对Properties进行校验了。这一步非常关键 Bean public static ConfigPropertiesValidator configurationPropertiesValidator() { return new ConfigPropertiesValidator(); }其中有两个注意点方法名必须为configurationPropertiesValidator 否则在启动的时候不会执行该校验方法必须声明为static, 告诉Spring该Bean的创建要在所有的Configuration之前我们修改在application.yml中config.nameconfig: name: a 这样name长度就不能满足我们自定义的校验规则启动程序查看结果***************************APPLICATION FAILED TO START***************************Description:Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under config to io.github.tinyking.springvalidation.config.ConfigProperties failed: Property: config.name Value: a Origin: class path resource [application.yml]:2:9 Reason: name is too shotAction:Update your applications configuration错误信息即为我们自定义校验的结果。4. 结论通过配置Spring Boot启动校验功能可以快速的识别参数配置的错误使我们在开发过程中有更好starter集成体验。