我是一个对Google Guice框架很新的人,我有一个关于在Guice Servlet中进行注入和使用RequestScope的问题。好的,让我通过我的代码举个例子来更清楚地说明这些事情。
我有一个名为Bean的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请求中,对吗?
欢迎任何帮助或示例。
谢谢 Br