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

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.
主站蜘蛛池模板: 枞阳县| 乐亭县| 湖州市| 汪清县| 绥德县| 邢台市| 苍南县| 崇信县| 滨州市| 措勤县| 古蔺县| 通山县| 灌南县| 凤庆县| 青龙| 曲松县| 石棉县| 公安县| 锡林浩特市| 永泰县| 绥中县| 南昌县| 永昌县| 九江县| 仁化县| 浪卡子县| 旺苍县| 凯里市| 建瓯市| 姚安县| 和平区| 嘉荫县| 临城县| 静乐县| 会东县| 新巴尔虎右旗| 睢宁县| 平利县| 太仆寺旗| 盐亭县| 新巴尔虎左旗|