>
> You might be able to use your own Guice-aware interpolator. I haven't
> tried to code this, so I may be totally wrong, but as you don't control
> object instantiation, you might need to make the Provider an ugly static
> field somewhere.
>
> Alternatively, you could configure the Validator and (perhaps)
> EntityManagerFactory programmatically.
>
> The last thing I can think of is that the JPA 2 spec provides a mechanism
> for this. Any injection it uses would probably be based on CDI, though.
>
Just for posterity's sake, here's the solution I came up with. In the
servlet initialization immediately after we get the Injector, I get the
EntityManagerFactory, open it up, and add my listeners. It's not too
pretty, but it does work.
private void insertValidationEventListeners(Injector injector) {
javax.inject.Provider<EntityManagerFactory> emfProvider =
injector.getProvider(EntityManagerFactory.class);
HibernateEntityManagerFactory hibernateEntityManagerFactory =
(HibernateEntityManagerFactory) emfProvider.get();
SessionFactoryImpl sessionFactoryImpl =
(SessionFactoryImpl)
hibernateEntityManagerFactory.getSessionFactory();
EventListenerRegistry registry =
sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class);
javax.inject.Provider<BeanValidationEventListener> listenerProvider
=
injector.getProvider(BeanValidationEventListener.class);
registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(listenerProvider.get());
registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(listenerProvider.get());
registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(listenerProvider.get());
}
and then my listener provider is
public class ValidationListenerProvider implements
Provider<BeanValidationEventListener> {
private ValidatorFactory
factory;
private Properties properties;
@Inject
public ValidationListenerProvider(ValidatorFactory
factory,
@Named("ValidationProperties") Properties properties) {
this.factory = factory;
this.properties = properties;
}
@Override
public BeanValidationEventListener get()
{
return new BeanValidationEventListener(factory, properties);
}
}
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.