RequestScoped
@RequestScoped
public class RequestScopedClass {
//This class gets constructed once per Servlet request, and is shared among all CDI-managed classes within that request.
@Inject
public RequestScopedClass(SomeDependency someDependency) {
doSomethingWith(someDependency);
}
public RequestScopedClass() {
//Note that it is required that a request scoped class have a public no-args constructor
}
}
如果 bean 使用 @RequestScoped 进行注释,则会为任何请求创建一次。如果两个对象依赖于请求范围类,则它们都将获得对同一对象的引用。
注意: 任何请求范围的 bean 必须具有公共的无参数构造函数。其原因将在后面解释。