根据xml配置校验bean

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

├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─honey
│  │  │          ├─collection
│  │  │          └─oval
│  │  └─resources

Userbean类,

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测试类,

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 constraintViolationList = guard.validate(user);
        System.out.println(constraintViolationList);
    }
}

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



    
    
        
        
            
        
    

    
    
    

        
            
        

        
            
        

        
        
            
            
        

        
            
            
        

        
        
            
            
                
            

            
            
            
        

        
        
            
            
                
            
        
    

输出结果,

[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]

你可能感兴趣的:(java,oval)