java-如何将Spring bean注入Validator(休眠)
作者:互联网
我已阅读以下春季文档:
By default, the LocalValidatorFactoryBean configures a
SpringConstraintValidatorFactory that uses Spring to create
ConstraintValidator instances. This allows your custom
ConstraintValidators to benefit from dependency injection like any
other Spring bean.
我写了自定义验证器:
public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>{
@Autowired
MyBeanDao dao;
...
}
但是在调试中,我看到dao为空.
请解释我不理解文档或我配置错误?
解决方法:
使用Spring Validator,它还实现Bean验证API.
请参见7.8.2配置Bean验证提供程序:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
您也可以将其用于方法验证,请参阅上面的文档中的“ Spring驱动的方法验证”部分.
[UPDATE1]
如果要让休眠验证持久性,则需要在enityManagerFactory中设置jpa属性javax.persistence.validation.factory:
<property name="jpaPropertyMap">
<map>
<entry key="javax.persistence.validation.factory" value-ref="validatorFactory" />
</map>
</property>
您可能还需要为事件设置验证组,请参阅:
http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-checkconstraints.html#validator-checkconstraints-orm-hibernateevent
标签:hibernate-validator,java,spring,bean-validation 来源: https://codeday.me/bug/20191013/1910496.html