【问题标题】:@Guarded not validating @NotNull constructor fields@Guarded 未验证 @NotNull 构造函数字段
【发布时间】:2015-07-10 09:27:14
【问题描述】:

我正在尝试使用 Oval 1.84 来绕过一些没有样板的验证约束。当我使用 @NotNull(javax.validation.constraint 和 net.sf.oval.validator)标记字段时,验证有效。

但是在对方法和构造函数参数实现约束的情况下不起作用。

【问题讨论】:

标签: java bean-validation oval


【解决方案1】:

参数验证需要使用一些方法调用拦截字节码。 OVal 为 AspectJ 和 Spring AOP 提供了现成的实现。

使用 AspectJ

http://oval.sourceforge.net/userguide.html#programming-by-contract 详细记录了如何将它与 AspectJ 一起使用

使用 Spring AOP

在测试用例中概述了 Spring AOP 的用法 https://svn.code.sf.net/p/oval/code/trunk/src/test/java/net/sf/oval/test/integration/spring/SpringAOPAllianceTest.java

在 Spring 中,您需要配置要对其进行方法参数验证的 bean,例如:

<bean id="myService" class="com.example.MyService" />

还有一个调用拦截器:

<bean id="ovalGuardInterceptor" class="net.sf.oval.guard.GuardInterceptor" />
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="proxyTargetClass" value="false" />
    <property name="interceptorNames">
        <list>
            <value>ovalGuardInterceptor</value>
        </list>
    </property>
    <!-- the next line tells which beans you want to use validation for -->
    <property name="beanNames" value="myService" />
</bean>

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2022-06-18
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多