我能够自己解决这个问题
创建提供者类 HibernateMetadataUtilProvider 以提供 HibernateMetadataUtil
package core.models.dao;
import javax.persistence.EntityManagerFactory;
import org.hibernate.ejb.HibernateEntityManagerFactory;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.googlecode.genericdao.search.MetadataUtil;
import com.googlecode.genericdao.search.jpa.hibernate.HibernateMetadataUtil;
@SuppressWarnings("deprecation")
public class HibernateMetadataUtilProvider
implements Provider<MetadataUtil> {
@Inject
private EntityManagerFactory emf;
@Override
public MetadataUtil get() {
return HibernateMetadataUtil.getInstanceForEntityManagerFactory((HibernateEntityManagerFactory) emf);
}
}
另一个类 JPASearchProcessorProvider 被创建,它将注入 JPASearchProcessor
package core.models.dao;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.googlecode.genericdao.search.MetadataUtil;
import com.googlecode.genericdao.search.jpa.JPASearchProcessor;
public class JPASearchProcessorProvider implements Provider<JPASearchProcessor> {
@Inject
private MetadataUtil mdu;
@Override
public JPASearchProcessor get() {
return new JPASearchProcessor(mdu);
}
}
然后在我们的注入类中,我们可以简单地绑定这些提供者
public class AppInjector extends AbstractModule {
@Override
protected void configure() {
/*
* JPA Bindings
*/
bind(JPASearchProcessor.class).toProvider(JPASearchProcessorProvider.class);
bind(MetadataUtil.class).toProvider(HibernateMetadataUtilProvider.class);
}