前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >根据xml配置校验bean

根据xml配置校验bean

作者头像
用户3579639
发布2018-10-22 15:01:59
5640
发布2018-10-22 15:01:59
举报
文章被收录于专栏:与神兽党一起成长

例子参考自官方文档,http://oval.sourceforge.net/u... 官方文档的xml配置有点过期了,一两个属性不合法。

代码语言:javascript
复制
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─honey
│  │  │          ├─collection
│  │  │          └─oval
│  │  └─resources

Userbean类,

代码语言:javascript
复制
package com.honey.oval;

public class User {
    private String firstName;
    private String lastName;
    private String managerId;
    private String userId;

    public User() {

    }
    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void setPasswordExpirationDays(int days) {

    }

}

OvalAppTest测试类,

代码语言:javascript
复制
package com.honey;

import com.honey.oval.User;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.configuration.xml.XMLConfigurer;
import net.sf.oval.guard.Guard;

import java.util.List;


public class OvalAppTest {

    public static void main(String[] args) {
        User user = new User("honey", "wang");
        XMLConfigurer xmlConfigurer = new XMLConfigurer(ClassLoader.class.getResourceAsStream("/oval-config.xml"));

        Guard guard = new Guard(xmlConfigurer);
        List<ConstraintViolation> constraintViolationList = guard.validate(user);
        System.out.println(constraintViolationList);
    }
}

xml配置,src/resources/oval-config.xml

代码语言:javascript
复制
<?xml version="1.0" ?>
<oval
        xmlns="http://oval.sf.net/oval-configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration.xsd"
>
    <!-- define a constraint set -->
    <constraintSet id="user.userid">
        <notNull />
        <matchPattern matchAll="false">
            <pattern pattern="^[a-z0-9]{8}$" flags="0" />
        </matchPattern>
    </constraintSet>

    <!-- define checks for the acme.model.User class -->
    <!-- overwrite=false means already defined checks for this class will not be removed -->
    <class type="com.honey.oval.User" overwrite="false" applyFieldConstraintsToSetters="true">

        <field name="firstName">
            <length min="0" max="3" />
        </field>

        <field name="lastName">
            <length min="0" max="5" />
        </field>

        <!-- overwrite=true means previously defined checks for this field will be overwritten by the checks defined here -->
        <field name="managerId" overwrite="true">
            <!-- use the checks defined for the constaint set "user.userid" -->
            <assertConstraintSet id="user.userid" />
        </field>

        <field name="userId" overwrite="true">
            <!-- use the checks defined for the constaint set "user.userid" -->
            <assertConstraintSet id="user.userid" />
        </field>

        <!-- define constructor parameter checks -->
        <constructor>
            <!-- parameter1 -->
            <parameter type="java.lang.String">
                <notNull />
            </parameter>

            <!-- parameter 2 -->
            <!-- the types of all parameters must be listed, even if no checks are defined -->
            <parameter type="java.lang.String" />
        </constructor>

        <!-- define method parameter checks -->
        <method name="setPasswordExpirationDays">
            <!-- parameter 1 -->
            <parameter type="int">
                <notNull />
            </parameter>
        </method>
    </class>
</oval>

输出结果,

代码语言:javascript
复制
[net.sf.oval.ConstraintViolation: com.honey.oval.User.managerId cannot be null, net.sf.oval.ConstraintViolation: com.honey.oval.User.firstName is not between 0 and 3 characters long, net.sf.oval.ConstraintViolation: com.honey.oval.User.userId cannot be null]
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档