Provider <httpsession>没有被注入</httpsession>

时间:2012-03-26 14:19:18

标签: gwt guice gwt-dispatch

我使用gwt dispatch进行通信并从服务器到客户端获取数据。

为了获取用户特定数据,我想将用户对象存储在httpsession中,并从servlet上下文访问应用程序特定数据

但是当我在调用处理程序执行时注入Provider<HttpSession>但是dispatchservlet时,提供程序为null,即它不会被注入。

以下是我的动作处理程序

中的代码
@Inject
Provider<HttpSession>   provider;

public ReadEntityHandler() {

} 

@Override
public EntityResult execute(ReadEntityAction arg0, ExecutionContext arg1) throws DispatchException {
    HttpSession session = null;
    if (provider == null)
        System.out.println("httpSession is null..");
    else {
        session = provider.get();
        System.out.println("httpSession not null..");
    }
    System.out.println("reached execution");
    return null;
}

和我的Dispatch servlet

@Singleton
public class ActionDispatchServlet extends RemoteServiceServlet implements StandardDispatchService {

    private Dispatch    dispatch;

            private Dispatch    dispatch;
@Inject ReadEntityHandler readEntityHandler;

public ActionDispatchServlet() {
    InstanceActionHandlerRegistry registry = new DefaultActionHandlerRegistry();
    registry.addHandler(readEntityHandler);
    dispatch = new SimpleDispatch(registry);
}


    @Override
    public Result execute(Action<?> action) throws DispatchException {
        try {
            return dispatch.execute(action);
        }
        catch (RuntimeException e) {
            log("Exception while executing " + action.getClass().getName() + ": " + e.getMessage(), e);
            throw e;
        }
    }
}

当我尝试注入ReadEntityHandler时,它会抛出以下异常

[WARN] failed guiceFilter
com.google.inject.ProvisionException: Guice provision errors:

1) Error injecting constructor, java.lang.NullPointerException
  at com.ensarm.wikirealty.server.service.ActionDispatchServlet.<init>(ActionDispatchServlet.java:22)
  at com.ensarm.wikirealty.server.service.ActionDispatchServlet.class(ActionDispatchServlet.java:22)
  while locating com.ensarm.wikirealty.server.service.ActionDispatchServlet

1 error
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:834)
    at  com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:856)
    at  com.google.inject.servlet.ServletDefinition.init(ServletDefinition.java:74)
    at com.google.inject.servlet.ManagedServletPipeline.init(ManagedServletPipeline.java:84)
    at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:106)
    at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:168)
    at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:593)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)
    at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
    at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)
    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)
    at com.google.gwt.dev.DevMode.main(DevMode.java:311)
Caused by: java.lang.NullPointerException
    at net.customware.gwt.dispatch.server.DefaultActionHandlerRegistry.addHandler(DefaultActionHandlerRegistry.java:21)
    at com.ensarm.wikirealty.server.service.ActionDispatchServlet.<init>(ActionDispatchServlet.java:24)
    at com.ensarm.wikirealty.server.service.ActionDispatchServlet$$FastClassByGuice$$e0a28a5d.newInstance(<generated>)
    at com.google.inject.internal.cglib.reflect.FastConstructor.newInstance(FastConstructor.java:40)
    at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:58)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:84)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:200)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:43)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:878)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.Scopes$1$1.get(Scopes.java:64)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:825)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:871)
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:821)
    ... 25 more

2 个答案:

答案 0 :(得分:0)

Caused by: java.lang.NullPointerException
    at net.customware.gwt.dispatch.server.DefaultActionHandlerRegistry.addHandler(DefaultActionHandlerRegistry.java:21)

DefaultActionHandlerRegistry是否有可能在第21行出现错误,并且错误发生在那里?我没有看到该类的代码,因此不清楚这是否是您的问题的原因,或仅仅是一个症状。

答案 1 :(得分:0)

您自己不是通过容器创建ReadEntityHandler registry.addHandler(new ReadEntityHandler());的实例。Provider<HttpSession> provider;为空

尝试:

@Inject
public ActionDispatchServlet(ReadEntityHandler handler) {
        InstanceActionHandlerRegistry registry = new DefaultActionHandlerRegistry();
        registry.addHandler(handler);
        dispatch = new SimpleDispatch(registry);
    }