The difference between context and scope in CDI -

2019-04-04 17:37发布

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.

标签: java scope cdi
3条回答
欢心
2楼-- · 2019-04-04 18:04

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.

查看更多
Ridiculous、
3楼-- · 2019-04-04 18:17

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,

get the userName from the session context
get the userName from the session scope

both sound fine, because we are talking about an execution on a variable.

The following example is intelligible per the definition of scope

the scope of the injected bean is Session

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 or Scope are pretty much interchangeable. If there's only one object representing a session, the class probably should be named just Session. If we split the part about variable manipulation, that part can be called SessionScope. However, the meaning of SessionContext 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.

查看更多
男人必须洒脱
4楼-- · 2019-04-04 18:18

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.

查看更多
登录 后发表回答