Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is meant by context in CDI?

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

like image 887
Krishna Chaitanya Avatar asked Jan 12 '23 08:01

Krishna Chaitanya


2 Answers

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

like image 187
awi Avatar answered Jan 21 '23 22:01

awi


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.

like image 44
Yuri Avatar answered Jan 21 '23 20:01

Yuri