Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

scope

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?

like image 597
brandizzi Avatar asked Jul 03 '11 19:07

brandizzi


2 Answers

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.

like image 113
Bozho Avatar answered Sep 19 '22 20:09

Bozho


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.

like image 31
meriton Avatar answered Sep 21 '22 20:09

meriton