The difference between context and scope in CDI - and Java at all

我只是一个虾纸丫 提交于 2019-12-30 05:56:09

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6565047/the-difference-between-context-and-scope-in-cdi-and-java-at-all

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!