Spring notes --Bean scope

Spring application context bean are all created in the form of a singleton (Singleton) a. In other words, regardless of a given bean is injected into another bean how many times the injected are the same instance.
However, there are times when a class is in different situations require different attributes, in this case, the class is declared as a singleton bean is not a good idea, because the object is contaminated, reused later time there will be unexpected problems

Spring defines a variety of scopes can be created based on these bean scope, including:

  • Singleton (Singleton): throughout the application, only create an instance of the bean.
  • Prototype (Prototype): each injection or acquired by the Spring application context
    that they will create a new bean instance.
  • Session (Session): In the Web application, create a real bean for each session
    patients.
  • Request (Rquest): In Web applications, to create a solid for each request bean
    embodiment.

Singleton is the default scope scope If other, to use @Scope annotation, it can
be used with or @Component @Bean.
Here Insert Picture Description

Published 25 original articles · won praise 22 · views 3639

Guess you like

Origin blog.csdn.net/weixin_42443419/article/details/103732008