CDI中的上下文是什么意思?

编程入门 行业动态 更新时间:2024-10-28 14:28:32
本文介绍了CDI中的上下文是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是CDI的新手。在阅读时,我总是遇到上下文对象,非上下文对象。它们是什么意思?

I am new to CDI. While reading, I am always encountering contextual objects, non contextual objects. What does they mean?

例如下面的链接

docs.jboss/weld/reference/latest/en-US/html /beanscdi.html#d0e881

消息驱动和实体bean本质上是非上下文对象和可能不会注入其他对象

Message-driven and entity beans are by nature non-contextual objects and may not be injected into other objects

推荐答案

CDI框架的上下文基本上很大对象图*。您可以使用任何CDI配置方法(spring xml bean /注解,如@ Component / @ Service),将对象添加到上下文中,或使CDI框架从服务类中创建对象。

The context of a CDI framework is basically a big map of objects*. You can add objects to the context or make the CDI framework create objects from your service classes by using any CDI configuration method (spring xml beans/annotations like @Component/@Service).

一旦有了上下文,就可以从中获取对象:(Spring:getBean(name))

Once you have the context you can get objects from it: (Spring: getBean(name))

现在,您可以在对象中配置对象/ bean之间的依赖关系了。上下文,并且CDI将确保从上下文中获取的任何对象都将设置其依赖项。这是依赖性注入部分。

Now you can configure dependencies between the objects/beans in the context, and the CDI will make sure any object you get from the context will have its dependencies set. This is the dependency injection part.

非上下文对象只是不添加到上下文中,CDI框架也不了解它们。通常只有服务类是CDI上下文的一部分。

Non-contextual objects are simply not added to the context and the CDI framework does not know about them. Usually only service classes are part of the CDI context.

*虽然不是真正的地图,但是可以通过名称,类型和其他方式访问对象。默认值是每次使用相同的名称(单例)询问时都会得到相同的对象,尽管您可以配置CDI在每次询问时(原型)创建一个新对象。

* Not really a map though, objects can be accessed by name, by type and other ways. The default is you get the same object each time you ask by the same name (singleton), although you may configure the CDI to create a new object each time you ask (prototype).

更多推荐

CDI中的上下文是什么意思?

本文发布于:2023-10-08 14:16:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   CDI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!