Questions tagged [cdi]

Contexts and Dependency Injection (CDI): Java Platform, Enterprise Edition (Java EE) 5 brought dependency injection (DI) with Convention over Configuration to Enterprise JavaBeans (EJB) 3.0. Java EE 6 introduces the flexible and powerful @Inject dependency injection model (JSR-330 and JSR-299) in addition to the already existing @EJB annotation.

Contexts and Dependency Injection (CDI) is a new Java EE 6 specification, which not only defines a powerful and type-safe Dependency Injection, but also introduces the concept of "contextual" references or scopes.

The "C" in CDI is the main difference between EJB beans and managed CDI beans. CDI-managed beans are contextual and EJB beans are not. Managed beans in CDI live in well-defined scope. They are created and destroyed on demand by the container. CDI comes already with pre-defined scopes and annotations :

  • @RequestScoped
  • @SessionScoped
  • @ApplicationScoped
  • @ConversationScoped.

The CDI container manages all beans inside the scope automatically for you. At the end of an HttpSession or HttpRequest, all instances associated with this scope are automatically destroyed and, thus, garbage collected.

This behavior is very different from that of Stateful session beans. A Stateful session bean instance needs to be explicitly removed by the client with the invocation of a method annotated with @Remove. It will not be automatically destroyed by the container; it is not bound to any context. If you associate a Stateful session bean with the HttpSession, you also have to care about its reliable destruction at the end or timeout of the HttpSession.

The contextual nature of CDI makes the use of beans from different scopes more natural and convenient. You can even mix and match scopes and inject beans from different scopes. The container will still care about proper lifecycle management.

4002 questions
411
votes
4 answers

Why use @PostConstruct?

In a managed bean, @PostConstruct is called after the regular Java object constructor. Why would I use @PostConstruct to initialize by bean, instead of the regular constructor itself?
Jan
  • 9,397
  • 13
  • 47
  • 52
167
votes
9 answers

Should I use @EJB or @Inject

I have found this question: What is the difference between @Inject and @EJB but I did not get any wiser. I have not done Java EE before nor do I have experience with dependency injection so I do not understand what I should use? Is @EJB an old way…
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
144
votes
18 answers

Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable

When trying to reference a managed bean in EL like so #{bean.entity.property}, sometimes a javax.el.PropertyNotFoundException: Target Unreachable exception is being thrown, usually when a bean property is to be set, or when a bean action is to be…
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
136
votes
3 answers

Where to use EJB 3.1 and CDI?

I am making a Java EE based product in which I'm using GlassFish 3 and EJB 3.1. My application has session beans, a scheduler and uses web services. I recently came to know about Apache TomEE, which supports Contexts and Dependency Injection (CDI).…
Dhrumil Shah
  • 2,128
  • 5
  • 23
  • 37
119
votes
6 answers

What is the difference between @ApplicationScoped and @Singleton scopes in CDI?

In CDI there is the @ApplicationScoped and the (javax.inject) @Singleton pseudo-scope. What is the difference between them? Besides the fact that @ApplicationScoped is proxied, and @Singleton is not. Can I just change my @Singleton bean to…
amorfis
  • 15,390
  • 15
  • 77
  • 125
116
votes
5 answers

Backing beans (@ManagedBean) or CDI Beans (@Named)?

I've just started reading through Core JavaServer Faces, 3rd Ed. and they say this (emphasis mine): It is a historical accident that there are two separate mechanisms, CDI beans and JSF managed beans, for beans that can be used in JSF pages. We…
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
111
votes
3 answers

How do CDI and EJB compare? interact?

I'm having a tough time understanding how the two interact and where the boundary between them lies. Do they overlap? Are there redundancies between them? I know there are annotations associated with both, but I haven't been able to find a complete…
Tim
  • 6,851
  • 11
  • 42
  • 46
109
votes
3 answers

Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean

I feel there is a little mess in the Java EE 6 spec. There are several sets of annotations. We have javax.ejb annotations like @Stateful and @Stateless for creating EJBs. There is also a @javax.annotation.ManagedBean to create a managed bean. There…
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
98
votes
7 answers

WELD-000072 Managed bean declaring a passivating scope must be passivation capable

I wrote a simple program in java web forms but i am receiving the following error: WELD-000072 Managed bean declaring a passivating scope must be passivation capable. Bean: Managed Bean [class BeanPakage.DemoBeans] with qualifiers [@Any @Default…
christina
  • 983
  • 1
  • 6
  • 6
73
votes
3 answers

Is CDI a good replacement of Spring?

We are planning to write a web application from the scratch, it has been decided to go with the latest edition of Glassfish which complies with Java EE 6 standard, therefore we are analyzing if CDI can be used instead of Spring. Can we say that CDI…
prassee
  • 3,651
  • 6
  • 30
  • 49
66
votes
3 answers

Inject and Resource and Autowired annotations

What's the difference between @Inject and @Resource and @Autowired annotations? When should we use each of them?
oxygenan
  • 1,023
  • 2
  • 12
  • 21
64
votes
3 answers

CDI: beans.xml, where do I put you?

I am using Weld as CDI implementation. My integration test, that tries to assemble object graph instantiating Weld container works well, when I have empty beans.xml in src/test/java/META-INF/beans.xml. Here is that simple test: public class…
Xorty
  • 18,367
  • 27
  • 104
  • 155
63
votes
6 answers

Google Guice vs. JSR-299 CDI / Weld

Weld, the JSR-299 Contexts and Dependency Injection reference implementation, considers itself as a kind of successor of Spring and Guice. CDI was influenced by a number of existing Java frameworks, including Seam, Guice and Spring. However, CDI…
deamon
  • 89,107
  • 111
  • 320
  • 448
60
votes
7 answers

WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default

I'm a Java EE-newbie. I want to test JSF and therefore made a simple program but can not deploy it. I get the following error message: cannot Deploy onlineshop-war deploy is failing=Error occurred during deployment: Exception while loading the app :…
mike128
  • 609
  • 1
  • 5
  • 8
59
votes
2 answers

Please explain the @Produces annotation in CDI

I have read about the @Produces annotation in CDI, but I don't understand its usage. public class Resources { // Expose an entity manager using the resource producer pattern @SuppressWarnings("unused") @PersistenceContext @Produces private…
user798719
  • 9,619
  • 25
  • 84
  • 123
1
2 3
99 100