首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring3.1 对Bean Validation轨范的新支持(方法级别验证)

2012-09-04 
Spring3.1 对Bean Validation规范的新支持(方法级别验证)?@RunWith(value SpringJUnit4ClassRunner.clas

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.

定义了基于注解方式的JavaBean验证元数据模型和API,也可以通过XML进行元数据定义,但注解将覆盖XML的元数据定义。}
这句话对吗?是不是应该为:XML的元数据定义会覆盖注解定义的
2 楼 jinnianshilongnian 2012-05-30   dzj2046 写道{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.

定义了基于注解方式的JavaBean验证元数据模型和API,也可以通过XML进行元数据定义,但注解将覆盖XML的元数据定义。}
这句话对吗?是不是应该为:XML的元数据定义会覆盖注解定义的

你试一下 按照上边说的 是 注解将继承并覆盖xml方式的

热点排行