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
52
votes
4 answers

Why use CDI in Java EE

I know there are a lot of articles out there that explain how to use CDI in Java EE but I'm having trouble figuring out what advantage this actually brings. For example, suppose I have a class that currently uses an instance of Foo. I might either…
PhilDin
  • 2,802
  • 4
  • 23
  • 38
43
votes
3 answers

Canonical way to obtain CDI managed bean instance: BeanManager#getReference() vs Context#get()

I figured that there are two general ways to obtain an auto-created CDI managed bean instance via BeanManager when having solely a Bean to start with (which is created based on Class): By BeanManager#getReference(), which is more often shown…
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
42
votes
1 answer

Why are there different bean management annotations

What is the difference between import javax.annotation.ManagedBean; import javax.enterprise.context.SessionScoped; and import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; ?
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
40
votes
2 answers

Inject list of objects in CDI (Weld)

Let's say I have an interface called SocialNetworkService, and three implementations - TwitterService, FacebookService and FriendFeedService. Now I want, whenever my managed bean (or whatever web component) receives a message, to share it in all…
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
39
votes
6 answers

How to programmatically inject a Java CDI managed bean into a local variable in a (static) method

How can I programmatically inject a Java CDI 1.1+ managed bean into a local variable in a static method?
XDR
  • 4,070
  • 3
  • 30
  • 54
39
votes
2 answers

How to install and use CDI on Tomcat?

I'm creating my first project Java EE 7, but I'm having trouble. Appreciate any help. Tomcat 7.0.34 JSF 2.2 Primefaces 3.5 javaee-api-7.0.jar When the application start, the Tomcat log shows the following message: "validateJarFile…
Rogério Arantes
  • 712
  • 1
  • 8
  • 29
36
votes
2 answers

@Inject, @EJB, @Local, @Remote, @LocalBean, etc... : confused?

I have the following configuration: 1 EAR on one GF containing 2 EJB-JARs with EJB components. 1 WAR on another Glassfish server (=> other JVM) containing web components accessing the EJB components. I have 2 EJB business services in each EJB-JAR…
Blaise Gosselin
  • 506
  • 1
  • 5
  • 7
35
votes
1 answer

What is Weld, JSR-299?

I've found lots of tutorials showing Weld code samples, but not an introductory overview. Could you please suggest an introductory article, or answer the following: What does Weld do/give you? How does it relate to Java EE 6? How would one use it…
retrodev
  • 2,323
  • 6
  • 24
  • 48
35
votes
10 answers

Java Dependency injection: XML or annotations

Annotations becoming popular. Spring-3 supports them. CDI depends on them heavily (I can not use CDI with out of annotations, right?) My question is why? I heard several issues: "It helps get rid of XML". But what is bad about xml? Dependencies are…
Ilya K
  • 413
  • 1
  • 5
  • 6
35
votes
3 answers

Why use constructor over setter injection in CDI?

I couldn't find any reasonable answer here on SO so I hope it's not a duplicate. So why should I prefer setter or constructor injection over simple @Inject MyBean bean; I get the usage of the constructor injection if you need to do something with…
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
35
votes
3 answers

Meaning of bean discovery mode annotated in CDI 1.1

I am migrating an application to Java EE 7 and would like to CDI 1.1. But I don't get the meaning of bean-discovery-mode="annotated". The CDI 1.1 specification is not very helpful. At least I have not found any useful paragraph. Did I miss it? This…
Oliver
  • 3,815
  • 8
  • 35
  • 63
32
votes
4 answers

CDI Injection into a FacesConverter

From just a few searches, this seems like a problem that has been around for a while. I have written a FacesConverter that looks like the following. The object Category is a JPA entity and CategoryControl is the DAO that fetches…
AlanObject
  • 9,613
  • 19
  • 86
  • 142
31
votes
1 answer

Will Spring support CDI?

Spring has been a good framework for DI and AOP for Java developers. Now, there comes a standard specification for DI and AOP which is CDI. Any good product can no longer stays good without supporting the relevant standards. (For example, Hibernate…
siva636
  • 16,109
  • 23
  • 97
  • 135
30
votes
3 answers

What are the best debugging tricks with Weld/CDI?

One of the beauties with Java EE 6 is the new dependency injection framework - CDI with the Weld reference implementation - which has prompted us to start migrating internally to JSR-330 in an implementation agnostic manner, with the explicit target…
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
30
votes
6 answers

Can @Inject be made optional in JSR 330 (like @Autowire(required=false)?

Spring's @Autowire can be configured such that Spring will not throw an error if no matching autowire candidates are found: @Autowire(required=false) Is there an equivalent JSR-330 annotation? @Inject always fails if there is no matching candidate.…
Eric B.
  • 23,425
  • 50
  • 169
  • 316