Search code examples
javascopecdi

The difference between context and scope


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?


Solution

  • 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.