在私有Guice模块中公开泛型绑定

时间:2013-03-14 13:27:45

标签: java persistence guice

我最近开始与Google Guice合作,通过我在网上找到的教程和代码,但现在我被卡住了。

我尝试创建一个GenericDAO,我可以基于每个EntityDAO进行扩展。我无法弄清楚如何配置现有的私有Guice模块。

以下是我的PrivateModule,其中Person是使用String身份的实体。 Address类是另一个不依赖于GenericDAO的实体(就在那时,我试着让它保持简单开始)。

public class PersistModule extends PrivateModule {

    @Override
    protected void configure() {
        install(new JpaPersistModule("jpa"));

        bind(PersistenceLifeCycleManager.class)
            .annotatedWith(PersistService.class)
            .to(PersistenceLifeCycleManagerImpl.class);
        expose(PersistenceLifeCycleManager.class)
            .annotatedWith(PersistService.class);

        bind(new TypeLiteral<GenericDAO<Person, String>>() {})
            .to(new TypeLiteral<PersonDao>() {});
        // How to expose this??

        bind(AddressDao.class).asEagerSingleton();
        expose(AddressDao.class);
        bind(EntityLocator.class).asEagerSingleton();
        expose(EntityLocator.class);
    }
}

尝试运行时我得到ConfigurationException

com.google.inject.ConfigurationException: Guice configuration errors:

1) Unable to create binding for com.guicetest.server.dao.PersonDao. It was already configured on one or more child injectors or private modules
   (bound by a just-in-time binding)
  If it was in a PrivateModule, did you forget to expose the binding?
  while locating com.guicetest.server.dao.PersonDao

1 error
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at com.guicetest.server.service.PersonService.getAllPersons(PersonService.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.web.bindery.requestfactory.server.ReflectiveServiceLayer.invoke(ReflectiveServiceLayer.java:182)
at com.google.web.bindery.requestfactory.server.ServiceLayerDecorator.invoke(ServiceLayerDecorator.java:111)
at com.google.web.bindery.requestfactory.server.ServiceLayerDecorator.invoke(ServiceLayerDecorator.java:111)
at com.google.web.bindery.requestfactory.server.ServiceLayerDecorator.invoke(ServiceLayerDecorator.java:111)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.processInvocationMessages(SimpleRequestProcessor.java:463)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.process(SimpleRequestProcessor.java:233)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.process(SimpleRequestProcessor.java:135)
at com.google.web.bindery.requestfactory.server.RequestFactoryServlet.doPost(RequestFactoryServlet.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
at com.guicetest.server.injector.PersistFilter.doFilter(PersistFilter.java:39)
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

Google Guice用户指南并没有真正帮助我,但我只是从它开始,所以也许我错过了一些东西,这很容易。

- 编辑 -

MyGuiceServletContextListner.java

public class MyGuiceServletContextListner extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new MyServletModule(),
            new PersistModule());
    }

}

MyServletModule.java

public class MyServletModule extends ServletModule {

    @Override
    protected void configureServlets() {
        filter("/*").through(PersistFilter.class);
        bind(ExceptionHandler.class).to(RequestFactoryExceptionHandler.class);
        bind(ServiceLayerDecorator.class).to(MyServiceLayerDecorator.class);
        Map<String, String> params = new HashMap<String, String>();
        params.put("symbolMapsDirectory", "WEB-INF/classes/symbolMaps/");
        serve("/gwtRequest").with(MyRequestFactoryServlet.class, params);
    }
}

GenericDAOImpl.java

public abstract class GenericDAOImpl<E extends AbstractEntity, P> implements GenericDAO<E, P> {

    @Inject
    @PersistService
    private PersistenceLifeCycleManager manager;
    @Inject
    private Provider<EntityManager> em;
    private Class<E> entityClass;

    @SuppressWarnings("unchecked")
    @Transactional
    @Override
    public P create(E entity) { ... }

    // etc ...

    @Transactional
    @Override
    public List<E> getAll() {
        List<E> list = null;
        TypedQuery<E> qry = (TypedQuery<E>) getEntityManager()
            .createNamedQuery(getEntityClass().getSimpleName()+".getAll", , getEntityClass());
        if (qry != null) {
            list = qry.getResultList();
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<E> getEntityClass() {
    if (entityClass == null) {
    Type type = getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType paramType = (ParameterizedType) type;
            entityClass = (Class<E>) paramType.getActualTypeArguments()[0];
            } else {
                throw new IllegalArgumentException("Could not guess entity class by reflection");
            }
        }
        return entityClass;
    }

}

PersonDao.java

public class PersonDao extends GenericDAOImpl<Person, String> {

    @Transactional
    public Person getPersonByEmailId(String emailId) { ... }

}

PersonService.java

public class PersonService {

    @Inject
    private Injector injector;

    // ...

    public List<Person> getAllPersons() {
        PersonDao pd = injector.getInstance(PersonDao.class);
        return pd.getAll();
    }

1 个答案:

答案 0 :(得分:2)

您可以在private module中公开TypeLiteral

TypeLiteral<GenericDAO<Person, String>> personDaoTypeLiteral = new TypeLiteral<GenericDAO<Person, String>>() {};
bind(personDaoTypeLiteral).to(PersonDao.class);
expose(personDaoTypeLiteral);

然后在PersonService中,由于模块公开了GenericDAO<Person, String>,因此无法获得PersonDAO的实例。

public class PersonService {

    @Inject
    private Injector injector;

    public List<Person> getAllPersons() {
        TypeLiteral<GenericDAO<Person, String>> personDaoTypeLiteral = new TypeLiteral<GenericDAO<Person, String>>() {};
        PersonDao pd = injector.getInstance(Key.get(personDaoTypeLiteral));
        return pd.getAll();
    }
}

请注意,代码不清楚这些类型文字。你可以使用常量:

public class Literals {
    public static final TypeLiteral<GenericDAO<Person, String>> PERSON_DAO = new TypeLiteral<GenericDAO<Person, String>>() {
    };
}

然后

bind(Literals.PERSON_DAO).to(PersonDao.class);
expose(Literals.PERSON_DAO);

injector.getInstance(Key.get(Literals.PERSON_DAO));