Search code examples
springspring-framework-beans

Spring framework scope - Is it a misnomer?


I know there is a term Bean scope in Spring framework, and for some reasons it is confusing me, mainly because of the term scope, because we have this terms (scope) in languages, like C, Java applied to a variable scope (that is where a variable is visible).

I know there are five bean scope, and I am not asking explanation on them, I am not clear on what Bean scope means. Can anyone help me understand what does this term mean?


Solution

  • In programming languages scope of the variables defines where in the code, the variable can be reached.

    • Global variables can be accesed from everywhere.
    • Function parameters or local variables can be accesed only in the function.

    In Spring framework scope of the bean defines when during the application runtime we are dealing with the same object.

    • Singleton scoped bean is an object that is unique to the whole application. Like global variable in programming languages.
    • Session scoped bean is an object that is unique to the session.
    • Request scoped bean is an object that is unique to the request. Like function parameters.
    • Prototype scoped bean is not unique to anything. Every time you get it, you have a new copy. Hard to compare but it can be a heap allocated variable.

    In computer science, the scope term is overloaded, the same as the term interface. You can have a Go or Java interface as well as PCI or ISA.

    Well, the term overload is also overloaded.