簡體   English   中英

Guice:如何創建HttpSessionProvider

[英]Guice: How to create HttpSessionProvider

我有一個模塊,下面定義為Servlet的內部類。

private static abstract class TestModule extends AbstractModule 
implements Provider<HttpSession> {

    @Override 
    protected void configure( ) {
     bind(HttpSession.class).toProvider( TestModule.class );
    }
    @Override public abstract HttpSession get( );
}

在Servlet的doGet()中,我創建了Injector,如下所示:

@Override 
protected void doGet( final HttpServletRequest req,
        HttpServletResponse resp ) throws ServletException, IOException {

    Injector injector = Guice.createInjector( new TestModule( ) {
        @Override 
        public HttpSession get( ) {
            return req.getSession( );
        } 
    });      
}

我收到錯誤:

1)沒有綁定javax.servlet.http.HttpSession的實現。

我究竟做錯了什么?

問題是你這樣配置:

bind(HttpSession.class).toProvider( TestModule.class );

但是HttpSession的實際提供者是您在創建注入器時創建的Anonymous內部類。

要解決此問題,只需使用Guice的ServletModule: http//code.google.com/p/google-guice/wiki/ServletModule

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM