本文介绍了提供者< HttpSession>没有注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using gwt dispatch to communicate and get data from server to client.

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

In order to get user specific data I want to store the user object in httpsession and access application specific data from servlet context

但是当我调用Provider<HttpSession>时,调用了处理程序execute,但是dispatchservlet的提供程序为null,即不会被注入.

but when I inject Provider<HttpSession> when the handler execute is called but the dispatchservlet the provider is null i.e it does not get injected.

以下是我的动作处理程序中的代码

following is the code from my action handler

@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

and my 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时,它将引发以下异常

when I try to inject the ReadEntityHandler it throws the following exception

[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

推荐答案

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

You create instance of ReadEntityHandler registry.addHandler(new ReadEntityHandler()); himself not by container.Provider<HttpSession> provider; is null

尝试:

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

这篇关于提供者&lt; HttpSession&gt;没有注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:13