Search code examples
javajava-ee-6cdi

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


Solution

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