How to inject service to hibernate.ejb.interceptor with google guice?

1.7k Views Asked by At

I have an issue with injecting a service to predefined interceptor using google guice.

What i'm trying to do is to use emptyinterceptor to intercept changes with entities. Interceptor itself works fine, the problem is that I can't figure out how to inject a service to it. Injections themselves work fine throughout the whole application.

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="db-manager">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>test.Address</class>
    <properties>
        <property name="hibernate.ejb.interceptor" value="customInterceptor"/>
    </properties>
</persistence-unit>

how im trying to inject

public class CustomInterceptor extends EmptyInterceptor {

private static final Logger LOG = Logger.getLogger(CustomInterceptor.class);

@Inject
private Provider<UploadedFileService> uploadedFileService;
...
}

how JpaPersistModule is initiated

public class GuiceListener extends GuiceServletContextListener {

private static final Logger LOG = Logger.getLogger(GuiceListener.class);

@Override
protected Injector getInjector() {
    final ServicesModule servicesModule = new ServicesModule();
    return Guice.createInjector(new JerseyServletModule() {
        protected void configureServlets() {

            // db-manager is the persistence-unit name in persistence.xml
            JpaPersistModule jpa = new JpaPersistModule("db-manager");

                            ...
                    }
            }, new ServicesModule());
     }
}

how services are initiated

public class ServicesModule extends AbstractModule {

@Override
protected void configure() {
    bind(GenericService.class).to(GenericServiceImpl.class);
    bind(AddressService.class).to(AddressServiceImpl.class);
}
}
1

There are 1 best solutions below

0
On

I searched for hours and have find no real solution, so the ugly workaround I am using is to create 2 interceptors.

The first one is bound correctly by hibernate but does not have anything injected. It calls the second interceptor via some other mechanism - in the example below via a static reference to the InjectorFactory. The second interceptor is not bound to Hibernate, but like any other class, it can happily have stuff injected into it.

//The first ineterceptor has methods like this...

@Override
  public synchronized void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
  InjectorFactory.getInjector().getInstance(MyOtherInterceptor.class).onDelete(entity, id, state, propertyNames, types);
}

d

//The second one has the real implementation

@Inject
public MyOtherInterceptor() {
}

@Override
public synchronized void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
  //Full implementation
}
//etc