Spring3.1 对Bean Validation规范的新支持(方法级别验证)
?
@RunWith(value = SpringJUnit4ClassRunner.class)@ContextConfiguration(value = {"classpath:spring-config-method-validator.xml"})public class MethodValidatorTest { @Autowired UserService userService; @Test public void testConditionSuccess() {//① 正常流程 userService.get2(1); } @Test(expected = org.hibernate.validator.method.MethodConstraintViolationException.class) public void testPreCondtionFail() { //②错误的uuid(即前置条件不满足) userService.get2(0); } @Test(expected = org.hibernate.validator.method.MethodConstraintViolationException.class) public void testPostCondtionFail() { //③不满足后置条件的返回值 userService.get2(10000); }}
通过如上测试,我们可以看出Spring3.1已经非常好的支持契约式编程了。
?
注意,在使用方法级别验证时:
1、由于Bean Validation1.1正处于草案状态,Spring3.1无法支持原生的Bean Validation1.1,在未来的Bean Validation1.1发布时会直接使用原生的。
2、Spring3.1需要使用Hibernate Validator 4.2及更高版本。
?
让我们期待Bean Validation 1.1的发布吧。
?
?
?
1 楼 dzj2046 2012-05-30 {This JSR will define a meta-data model and API for JavaBeanTM validation based on annotations, with overrides and extended meta-data through the use of XML validation descriptors.