在研究 JSR-299 时,我阅读了 焊接参考,它解释了范围在 CDI 中的工作原理。显然,上下文是一个与范围密切相关的概念。我的脑海中并不清楚其中的区别,我什至想互换使用这些词。
范围和上下文有什么区别?这两个概念之间有何关系?
Studying JSR-299, I read section 5.1 of the Weld reference which explains how scopes work in CDI. Apparently, context is a concept closely related to scope. The difference is not clear in my mind and I feel tempted to even use the words interchangeably.
What is the difference between scope and context? What is the relationship between the two concepts?
发布评论
评论(3)
每个范围都有一个单独的上下文。
上下文是已使用相应范围定义的 bean 的容器。
这就是为什么上下文实现带有范围名称 -
ApplicationContext
、DependentContext
、RequestContext
等。这实际上是一个实现细节 - 作为用户对于 CDI,您应该只了解范围(因为您为您的 bean 定义了范围),并且实现决定了这些 bean 的放置位置以及如何管理它们的生命周期。
Each scope has a separate context.
The context is a container for beans which have been defined with the respective scope.
That's why context implementations carry the name of the scope -
ApplicationContext
,DependentContext
,RequestContext
, etc.This is an implementation detail actually - as a user of CDI you should only know about scopes (because you define it for your beans), and the implementation decides where to place those beans and how to manage their lifecycle.
我的理解是,范围是指可以从何处访问对象,而上下文则枚举可以从程序执行中的某个特定点访问的对象。 (也就是说,我们讨论对象的范围,以及程序执行中某个特定点的上下文。)
从数学上来说,两者都描述了可以访问关系,但从不同的方向来看。
My understanding is that scope refers to where an object may be accessed from, while context enumerates the objects that can be accessed from some particular point in program execution. (That is, we talk about the scope of an object, and the context at some particular point in program execution.)
Mathematically speaking, both describe the can-access relation, but look at it in different directions.
首先,我们脑子里有应用程序、会话、请求等概念。让我们在以下示例中使用会话概念。
如果我们认为某个执行片段是为特定会话服务的,我们会说该会话是执行上下文的一部分;或者,它是执行的会话上下文。
会话有一些变量,例如
userName
;我们会说会话是这些变量的范围。由于两者都指向同一个会话,因此可能会造成混乱。例如,
两者听起来都很好,因为我们正在讨论变量上的执行。
根据范围的定义,以下示例是可以理解的
,但我们可以轻松理解到底发生了什么。如果我们愿意的话,我们可以扩展它,直到它基于单词的基本用法;我们不这样做,因为这会非常冗长。
作者面临着一项艰巨的任务:既要简洁地表达文字,又希望读者能以某种方式理解复杂的含义。对于那些不理解这些概念的人来说,有关上下文和范围的文本通常看起来是胡言乱语。
API名称更难起,因为代码不是英文句子。
Context
或Scope
几乎可以互换。如果只有一个对象代表一个会话,则该类可能应该仅命名为Session
。如果我们拆分有关变量操作的部分,该部分可以称为SessionScope
。然而,SessionContext
的含义太难以捉摸,仅从名称中我们就可以看出,它与会话有关 - 这里的“上下文”几乎是一个脏话。First, we have concepts in our heads like applications, sessions, requests. Let's use the session concept in the following examples.
If we consider that a piece of execution is serving for a particular session, we'll say the session is part of the context of the execution; or, it is the session context of the execution.
A session has some variables, e.g.
userName
; we'll say the session is the scope of these variables.Since both are pointing to the same session, it can get confusing. For example,
both sound fine, because we are talking about an execution on a variable.
The following example is intelligible per the definition of scope
but we don't have problem understanding what's really going on. If we want to, we can expand it till it's based on basic usages of words; we don't do that because it will be very verbose.
An author faces the difficult task of packing the words succinctly yet expecting readers somehow understand the complex meaning. Texts about context and scope usually appear to be gibberish to those who haven't understood the concepts.
API names are even more difficult to come up with, because codes are not English sentences.
Context
orScope
are pretty much interchangeable. If there's only one object representing a session, the class probably should be named justSession
. If we split the part about variable manipulation, that part can be calledSessionScope
. However, the meaning ofSessionContext
is too elusive, the best we can tell, from the name alone, is that it is about something of a session - "context" here is pretty much an expletive.