- Developing Middleware in Java EE 8
- Abdalla Mahmoud
- 579字
- 2021-07-23 19:24:33
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.
- 零基礎學Visual C++第3版
- C語言程序設計(第2 版)
- Azure IoT Development Cookbook
- Learn Swift by Building Applications
- Implementing Cisco Networking Solutions
- Magento 1.8 Development Cookbook
- FFmpeg入門詳解:音視頻原理及應用
- 微信小程序開發與實戰(微課版)
- 詳解MATLAB圖形繪制技術
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- Web Developer's Reference Guide
- HTML5移動前端開發基礎與實戰(微課版)
- Python網絡爬蟲實例教程(視頻講解版)
- Data Manipulation with R(Second Edition)
- 企業級Java現代化:寫給開發者的云原生簡明指南