官术网_书友最值得收藏!

Specifying a bean scope

CDI beans, according to the specification, are described to be contextual. By contextual it's meant that each CDI bean has a well-defined scope that it lives in. A scope describes the lifetime of the bean, that is, when the CDI bean shall be created and when it shall be destroyed. To make this more clear, consider the earlier examples where we have injected our beans into a servlet. The question is: will I obtain the same instance of the bean each time I run the example? Or will I obtain a new instance each time? Or one instance per user? Or what? The basic answer to all these questions, is specifying our bean scope.

One of the most popular examples of a bean scope that I'm pretty sure you have a prior knowledge of the singleton pattern. In this pattern, some class is supposed to have one, and only one, an instance of it in runtime. It should be created once, the first time it's required for usage, and destroyed whenever it's no longer useful or on the termination of our application. Such a pattern is one of our options when specifying the bean's scope property.

In the following list, we will list all available CDI scopes, alongside their annotation used, and the duration it lives in:

  • Request Scope: By using the request -scope, a new instance of the bean is created for each:
    • Request to the servlet, JSP, or JSF page
    • Call to remote EJB
    • Call to a web service

If multiple instances of the bean are injected by other beans, only one instance of the request-scoped bean is really created within the boundary of the user request in the cases we just listed and will be destroyed by the end of the user request. The following example shows how to make a CDI bean request-scoped:

@RequestScoped 
public class MyPojo { ... } 
  • Session scope: By using the session-scope, a new instance of the bean is created for each user session. If multiple instances of the bean are injected by other beans, only one instance of the session-scoped bean is really created within the boundary of the user's HTTP session and will be destroyed by the expiration of the user's session. The following example shows how to make a CDI bean session-scoped:
@RequestScoped 
public class MyPojo { ... } 
  • Application scope: By using the application-scope, only one instance of the bean is created for the entire application. If multiple instances of the bean are injected by other beans, only one instance of the application-scoped bean is really created globally within the entire application and will be destroyed when the application shuts down. The following example shows how to make a CDI bean application-scoped:
@RequestScoped 
public class MyPojo { 
  • Dependent scope: By using the dependent-scope, a new instance of the bean is created for each injection point. If multiple instances of the bean are injected by other beans, a new instance of the dependent-scoped bean is really created within the boundary of the scope of the owner's bean, and will be destroyed with it. Instances will never be shared across different injection points. The following example shows how to make a CDI bean session-scoped:
@RequestScoped 
public class MyPojo { ... } 
  • Conversation scope: By using the conversation-scope, a new instance of the bean is created for each maintained conversation. Conversations are introduced in JSF 2, and is out of the scope of this book.
主站蜘蛛池模板: 唐山市| 怀安县| 正安县| 荔浦县| 礼泉县| 共和县| 阜新市| 黄骅市| 兰坪| 寿阳县| 和林格尔县| 穆棱市| 扎赉特旗| 化隆| 原阳县| 白城市| 南漳县| 青铜峡市| 潼关县| 南京市| 承德县| 科尔| 连山| 卢湾区| 桦南县| 芮城县| 顺平县| 海城市| 汝阳县| 陈巴尔虎旗| 增城市| 嘉峪关市| 枣庄市| 益阳市| 临高县| 防城港市| 双柏县| 新田县| 富源县| 绥芬河市| 南丹县|