1 Reply Latest reply on Jul 12, 2011 11:14 AM by jnt30

    RF4+Spring+Hibernate validator

    vrelits

      Hi!

       

      I'm writing custom bean validators (JSR 303) and want to @Autowired dependencies into them.

       

      It works fine if I use it with spring wired Validator:

       

      @Autowired

      private Validator validator;

       

      With Richfaces, however, the injection of @Autowired does not work.

       

      I guess it somehow does not pick up the SpringContraintValidatorFactory.

       

      I'm not sure how I get Richfaces to use this factory.

       

      Currently my workaround is to do it manually:

       

      public void initialize(ValidateAuthorisations constraintAnnotation) {

              if (autowiredBean == null) {

                  FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance()).getAutowireCapableBeanFactory()

                          .autowireBean(this);

              }

      }

       

      but of couse, I would like to get rid of this code.

       

      Any suggestions?

        • 1. Re: RF4+Spring+Hibernate validator
          jnt30

          I went about this a bit different because I could not come up with the solution you were able to, using the SpringConstraintValidationFactory.

           

          I wrote a custom javax.validation.ConstraintValidatorFactory that also implements org.springframework.context.ApplicationContextAware.  In the com.company.validation.CustomConstraintValidationFactory.setApplicationContext(ApplicationContext) method I simply save off the application context.  When a call is made to com.company.validation.CustomConstraintValidationFactory.getInstance(Class<T>) looking for a constraint validator, I first check to see if Spring is managing an instance of a bean that fits the class signature.  If it does, I return the Spring bean that is autowired up.  If it does not, I delegate to teh regular Validation.byDefaultProvider()'s implmenetation for looking up the validator.

           

          You can customize this setting through the validation.xml that is located in the META-INF directory.  More information can be found at: http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/#d0e1867