【发布时间】:2011-05-25 07:49:44
【问题描述】:
我是 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 请求,对吗?
欢迎任何帮助或示例。 谢谢
【问题讨论】:
标签: java dependency-injection guice