Java Bean 验证

Bean 验证概述

Bean Validation 通过约束声明和 Java 应用程序的元数据提供了一种通用的验证方式。要使用它,您可以使用声明性验证约束来注释域模型属性,然后由运行时强制执行。

Bean Validation 允许您声明约束,如以下示例所示:

public class PersonForm {

    @NotNull
@Size(max=64)
private String name; @Min(0)
private int age;
}

一个 Bean Validation 验证器然后根据声明的约束来验证这个类的实例

配置 Bean 验证提供程序

Spring 为 Bean Validation API 提供全面支持,包括将 Bean Validation 提供者引导为 Spring bean。

您可以使用 将LocalValidatorFactoryBean默认验证器配置为 Spring bean,如以下示例所示:

import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class AppConfig { @Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
}

前面示例中的基本配置触发 bean 验证以使用其默认引导机制进行初始化。

注入验证器

如果您更喜欢直接使用 Bean Validation API,您可以注入一个引用,如以下示例所示:

import javax.validation.Validator;

@Service
public class MyService { @Autowired
private Validator validator;
}

配置一个DataBinder

从 Spring 3 开始,您可以DataBinder使用Validator. 配置完成后,您可以通过调用Validator来调用binder.validate(). 任何验证都会 Errors自动添加到活页夹的BindingResult.

以下示例显示了如何DataBinder在绑定到目标对象后以编程方式调用验证逻辑:

Foo target = new Foo();
DataBinder binder = new DataBinder(target);
binder.setValidator(new FooValidator()); // bind to the target object
binder.bind(propertyValues); // validate the target object
binder.validate(); // get BindingResult that includes any validation errors
BindingResult results = binder.getBindingResult();

Spring MVC 3 验证

请参阅Spring MVC 章节中的验证

Spring系列之验证-14的更多相关文章

  1. Spring系列14:IoC容器的扩展点

    Spring系列14:IoC容器的扩展点 回顾 知识需要成体系地学习,本系列文章前后有关联,建议按照顺序阅读.上一篇我们详细介绍了Spring Bean的生命周期和丰富的扩展点,没有阅读的强烈建议先阅 ...

  2. 朱晔和你聊Spring系列S1E2:SpringBoot并不神秘

    朱晔和你聊Spring系列S1E2:SpringBoot并不神秘 [编辑器丢失了所有代码的高亮,建议查看PDF格式文档] 文本我们会一步一步做一个例子来看看SpringBoot的自动配置是如何实现的, ...

  3. Spring系列15:Environment抽象

    本文内容 Environment抽象的2个重要概念 @Profile 的使用 @PropertySource 的使用 Environment抽象的2个重要概念 Environment 接口表示当前应用 ...

  4. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  5. Spring 系列教程之容器的功能

    Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...

  6. Spring 系列教程之自定义标签的解析

    Spring 系列教程之自定义标签的解析 在之前的章节中,我们提到了在 Spring 中存在默认标签与自定义标签两种,而在上一章节中我们分析了 Spring 中对默认标签的解析过程,相信大家一定已经有 ...

  7. Spring 系列教程之默认标签的解析

    Spring 系列教程之默认标签的解析 之前提到过 Spring 中的标签包括默认标签和自定义标签两种,而两种标签的用法以及解析方式存在着很大的不同,本章节重点带领读者详细分析默认标签的解析过程. 默 ...

  8. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  9. java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式

    Spring系列之 配置文件的操作 写在文章前面: 本文带大家掌握Spring配置文件的基础操作以及带领大家理清依赖注入的概念,本文涉及内容广泛,如果各位读者耐心看完,应该会对自身有一个提升 Spri ...

  10. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

随机推荐

  1. 使用IntelliJ创建第一个简单的Springboot项目

    使用Intellij创建Springboot项目 使用IntelliJ创建Springboot项目是非常方便的,创建过程中,我们可以自由的选择要启用的springboot生态里的各种框架.插件.本文只 ...

  2. Katalon-获取文本内容和预期文本对比(get Test)

  3. 网页端微信小程序客服

    https://mpkf.weixin.qq.com/ 可以设置自动回复

  4. PaddlePaddleOCR安装教程

    1.首先下载并安装python3.7.8 链接地址: 注:安装时Add path勾选 2.进入命令提示符界面 3.升级pip版本 python -m pip install --upgrade pip ...

  5. https原理(三)双向实践(curl)

    接 https代理服务器(三)实践,实践双向ssl 本文采用客户端与服务端不同密钥对 1 mkcert myclient 生成客户端公钥 私钥 2 mkcert -pkcs12 myclient 也可 ...

  6. linux 安装 talib 的完美姿势!

    安装 TA-Lib $ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz $ tar -zxvf ta-li ...

  7. debian(deepin)/ubuntu 安装 mysql5.7

    debian(deepin)/ubuntu 安装mysql5.7 Mysql安装 一.下载安装包 参考博客 https://blog.csdn.net/qq_44231964/article/deta ...

  8. Java注解及应用原理

    视频地址:https://www.bilibili.com/video/BV1Py4y1Y77P/?spm_id_from=333.337.search-card.all.click&vd_s ...

  9. windows 服务 包装模板

    github地址:  https://github.com/xl711436/Xiaolei.MockService 在 MockServiceInstance.cs 中 对应的方法中添加 对应的逻辑 ...

  10. git 与远程仓库关联返回 fatal: remote origin already exists 解决方法

    今天领导新建了一个代码仓库,我按照流程一步步推送代码,结果到了关联仓库的时候,返回 fatal: remote origin already exists(远程源已经存在) 下面是解决方法 1.删除远 ...