Annotation Type Scope


@Retention(CLASS) @Target({TYPE,METHOD}) public @interface Scope

How many instances of a bean there are, and how long each lives.

  • singleton (the default) -- one, for the life of the server.
  • prototype -- a new one for every injection point.
  • request -- one per HTTP request, built when the request first uses it.
  • session -- one per HttpSession.

A request or session bean injected into a singleton is reached through a small class the build generates, which finds the current request's instance on each call. That class extends the bean's, so a request or session bean cannot be final, needs a constructor taking no arguments, and has that constructor run once at start-up for the stand-in -- per-request set-up belongs in @PostConstruct, which runs on each real instance.

  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    One of singleton, prototype, request or session.
  • Element Details

    • value

      String value
      One of singleton, prototype, request or session.