1

spring doc

I have read the following spring documentation:

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.

I have wrote custom validator:

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>{
    @Autowired
    MyBeanDao dao;
    ...
}

But in debug I see that dao is null.

Please, explain I didn't understand documentation or I wrong configured something?

1 Answer 1

1

Use Spring Validator, it also implements Bean Validation API.

See 7.8.2 Configuring a Bean Validation Provider:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

You can also use it for method validations, see "Spring-driven Method Validation" section in the documentation above.

[UPDATE1]

If you want hibernate to validate on persist you need to set jpa property javax.persistence.validation.factory in your enityManagerFactory:

            <property name="jpaPropertyMap">
              <map>
                <entry key="javax.persistence.validation.factory" value-ref="validatorFactory" />
              </map>
            </property>

You may also need to set validation groups for events, see: http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-checkconstraints.html#validator-checkconstraints-orm-hibernateevent

1
  • Will spring validator automatically validate beans before persist? Jun 8, 2015 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.