We do it like this, but i don't understand fully why it is working
:) class SomeGuiceModule { protected void configure() { bind(Valdiator.class).to(ValidatorProvider.class); } } /** * This provider returns a validator for Hibernate validation annotations. */ @Singleton public class ValidatorProvider implements Provider<Validator>, Serializable { private static final long serialVersionUID = 1L; private final Validator validator; /** * Constructor. */ public ValidatorProvider() { final ValidatorFactory validatorFactory = Validation.byDefaultProvider() .configure() .messageInterpolator(new CustomMessageInterpolator()) .constraintValidatorFactory(new InjectingConstraintValidatorFactory()) .buildValidatorFactory(); validator = validatorFactory.getValidator(); } /** * @return a validator instance. */ @Override public Validator get() { return validator; } private static class InjectingConstraintValidatorFactory implements ConstraintValidatorFactory { @Override public final <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { final T constraintValidator = ReflectionHelper.newInstance(key, "ConstraintValidator"); InjectorHolder.getInjector().injectMembers(constraintValidator); return constraintValidator; } } private static class CustomMessageInterpolator implements MessageInterpolator { /** * {@inheritDoc} */ @Override public String interpolate(String messageTemplate, Context context) { return getLocalizedValue(messageTemplate); } /** * {@inheritDoc} */ @Override public String interpolate(String messageTemplate, Context context, Locale locale) { return getLocalizedValue(messageTemplate); } private String getLocalizedValue(String messageTemplate) { // Get rid of braces: {javax.validation.constraints.NotNull.message} --> javax.validation.constraints.NotNull.message final String messageTemplateWithoutBraces = messageTemplate.substring(1, messageTemplate.length() - 1); return Localizer.get().getString(messageTemplateWithoutBraces, null); } } } On 04/02/2014 08:08 AM, Moandji Ezana
wrote:
-- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. Visit this group at http://groups.google.com/group/google-guice. For more options, visit https://groups.google.com/d/optout. |
- Providing Injection in Classes built by Java Validation (J... Alex Wood
- Re: Providing Injection in Classes built by Java Vali... Moandji Ezana
- Re: Providing Injection in Classes built by Java ... Stephan Classen
- Re: Providing Injection in Classes built by J... Moandji Ezana
- Re: Providing Injection in Classes built by Java ... Alex Wood