Studying JSR-299, I read the section 5.1 of the Weld reference which explains how scopes works in CDI. Apparently, context is a concept closely related to scope. I have understood a bit about what each one is but it is not very clearly separated 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?
I expect an answer in the CDI domain but it is a doubt I have about Java in general, since the concepts are used in other contexts as well. Actually, an answer which explains the concepts in the context of CDI, Java and in general would get more points - if I could give them.
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.
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.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.