what is meant by context in CDI?

▼魔方 西西 提交于 2019-12-06 05:54:24

问题


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

For example the below link

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

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


回答1:


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).

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

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.

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.

* 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).




回答2:


A context in CDI is some span during execution of your program when contextual objects can be used. It defines when CDI container creates, destroys and how it links instances of those objects together.

Non-contextual objects are those that are not tied to any CDI context.

MDBs are one example, they are managed by EJB container and not intended to be used as ordinary objects. Entities come and go as you interact with a DB via JPA, so they also cannot be tied to a context. Another example is any object whose instances you create manually.



来源:https://stackoverflow.com/questions/21452132/what-is-meant-by-context-in-cdi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!