简体   繁体   English

CDI 中的 C 到底是什么?

[英]What exactly is C in CDI?

All the materials I saw on CDI were confusing or hard to understand about the nature of the Contexts in the CDI.我在 CDI 上看到的所有材料都对 CDI 中上下文的性质感到困惑或难以理解。

Contexts: The ability to bind the lifecycle and interactions of stateful components to well-defined but extensible lifecycle contexts上下文:将有状态组件的生命周期和交互绑定到定义明确但可扩展的生命周期上下文的能力

This definition suggests that contexts have something to do with lifecycle;这个定义表明上下文与生命周期有关;

A key part of CDI aside of its DI capabilities is its awarness of bean contexts and the management of bean lifecycle and dependencies within those contexts (such as @RequestScoped or @ConversationScoped).除了 DI 功能之外,CDI 的一个关键部分是它对 bean 上下文的感知以及对这些上下文中 bean 生命周期和依赖关系的管理(例如 @RequestScoped 或 @ConversationScoped)。

After I read this explanation several times, I came to the conclusion, that it suggests that context is a place where beans can be injected;多次阅读这个解释后,我得出的结论是,它表明上下文是可以注入 bean 的地方; such as servlet container, ejb container or some Java SE container.例如 servlet 容器、ejb 容器或一些 Java SE 容器。

Is this a correct conclusion?这是一个正确的结论吗?

I also have difficulties to understand what is the context in the first place.我也很难首先理解什么是上下文。 Let me try to explain it in my own words.让我试着用我自己的话来解释它。

I usually think that a context is an object representing the environment of a process.我通常认为上下文是表示进程环境的对象。 Just like the environment of a bash shell and the OS which internally contain many environmental variables.就像 bash shell 的环境和内部包含许多环境变量的操作系统一样。 It serves like a kind of global variables container that the process can access its variables easily at any time throughout the process lifecycle.它就像一种全局变量容器,流程可以在整个流程生命周期中随时轻松访问其变量。

Ideally, environment should be isolated with each other.理想情况下,环境应该彼此隔离。 Different process has their own isolated environment to work with such that a process cannot messed up with each other 's environment.不同的进程有他们自己的隔离环境来工作,这样一个进程就不会被彼此的环境搞乱。 Think that if I open two bash shell and I create a new variable in the shell1 , shell2 should not aware of this variable in their environment.认为如果我打开两个 bash shell 并在 shell1 中创建一个新变量,shell2 不应该知道它们环境中的这个变量。

In CDI, the process can be running an application, handling a HTTP request , handling a HTTP session or handling a conversation etc. which each type of process has their own context type which is application context , request context , session context and conversation context respectively.在 CDI 中,进程可以运行应用程序、处理 HTTP 请求、处理 HTTP 会话或处理会话等,每种类型的进程都有自己的上下文类型,分别是应用程序上下文、请求上下文、会话上下文和会话上下文. Each context internally can only contain object which the scoped is the same as the scope supported by that context (eg Application context store @ApplicationScoped bean etc.)每个上下文内部只能包含范围与该上下文支持的范围相同的对象(例如应用程序上下文存储@ApplicationScoped bean 等)

What CDI helps is to manage an environment for these process such that each process has their own isolated environment. CDI 的帮助是管理这些进程的环境,以便每个进程都有自己的隔离环境。 For example, in case of session context , it internally keep all the @SessionScoped bean .例如,在 session context 的情况下,它在内部保留所有@SessionScoped bean。 CDI ensures that each HTTP session access their own isolated context correctly behind scene such thus the correct @SessionScoped beans are injected into the codes. CDI 确保每个 HTTP 会话在幕后正确访问他们自己的隔离上下文,因此正确的@SessionScoped bean 被注入到代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM