CDI中的上下文和范围之间的区别 - 以及Java [英] The difference between context and scope in CDI - and Java at all

查看:123
本文介绍了CDI中的上下文和范围之间的区别 - 以及Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习JSR-299,我阅读了焊接参考,解释了范围在CDI中的工作原理。显然,语境是与范围密切相关的概念。我已经理解了每一个是什么,但在我的脑海里并没有非常清楚地分开,我甚至想要交替使用这些词。

Studying JSR-299, I read the section 5.1 of the Weld reference which explains how scopes works in CDI. Apparently, context is a concept closely related to scope. I have understood a bit about what each one is but it is not very clearly separated in my mind and I feel tempted to even use the words interchangeably.

有什么区别范围和背景之间?这两个概念之间的关系是什么?

What is the difference between scope and context? What is the relationship between the two concepts?

我希望在CDI域中得到一个答案但是我对一般的Java有疑问,因为这些概念用于其他情况也是如此。实际上,在CDI,Java 的上下文中解释概念的答案通常会得到更多的分数 - 如果我可以给它们。

I expect an answer in the CDI domain but it is a doubt I have about Java in general, since the concepts are used in other contexts as well. Actually, an answer which explains the concepts in the context of CDI, Java and in general would get more points - if I could give them.

推荐答案

每个范围都有一个单独的上下文。

Each scope has a separate context.

上下文是使用相应范围定义的bean的容器。

The context is a container for beans which have been defined with the respective scope.

这就是上下文实现带有范围名称的原因 - ApplicationContext DependentContext RequestContext 等。

That's why context implementations carry the name of the scope - ApplicationContext, DependentContext, RequestContext, etc.

这实际上是一个实现细节 - 作为CDI的用户你应该只知道范围(因为你为bean定义了它),实现决定了这些bean的放置位置以及如何管理它们的生命周期。

This is an implementation detail actually - as a user of CDI you should only know about scopes (because you define it for your beans), and the implementation decides where to place those beans and how to manage their lifecycle.

这篇关于CDI中的上下文和范围之间的区别 - 以及Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆