我是Google Guice框架中的新成员,我有一个关于在guice servlet中注入和使用RequestScope的问题。好的,让我从我的代码中举一些例子来明确事情。Guice在servlet中注入
我有例如豆bean类..
@RequestScope
public class Bean {
private String user;
private String pass;
// constructor which is @inject
// getters and setters
}
在这里,我已经有了一个servlet
@Singleton
public class MainServlet extends HttpServlet {
doGet(HttpServletRequest request, HttpServletResponse response) {
.... some code
Injector injector = Guice.createInjector();
ValidUser validUser = injector.getInstance(ValidUser.class)
// Here i got the below exception
}
}
com.google.inject.ConfigurationException: Guice configuration errors:
1) No scope is bound to com.google.inject.servlet.RequestScoped.
at Bean.class while locating Bean
有趣这里的servlet范围是单身,因为我们知道。 而且我怎样才能从http请求 - Bean实例?因为据我了解,一个Bean类的实例被注入后,它会进入http请求,对吧?
欢迎任何帮助或示例。 感谢 溴
谢谢,我想我明白了。 :) – brakebg 2011-05-27 07:12:02