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
23
votes
1 answer

IDEA error "Managed bean must be a concrete class, or is annotated with @Decorator"

In my Java EE project I have a few abstract classes. IntelliJ IDEA underlines them red and tells me: Managed bean must be a concrete class, or is annotated with @Decorator A top-level Java class is a managed bean if it is defined to be a managed…
Adrian Krebs
  • 4,179
  • 5
  • 29
  • 44
23
votes
4 answers

CDI : WELD-001408 Unsatisfied dependencies, how to resolve it?

I do a small test project with CDI. My application is composed of an EJB EAR and WAR, all deployed on Glassfish 4. I'm using Hibernate 4.3.4 to access the database. My goal is to verify that a class in an EJB (DAO) can receive an injection of an…
Scandinave
  • 1,388
  • 1
  • 17
  • 41
23
votes
3 answers

Best practice for serialization for EJB and CDI beans

I have not yet experienced any serialization-related issues. But PMD and Findbugs detect a bunch of potential problems regarding seriazation. A typical case is an injected logger that is being detected as non-serializable. but there are many more -…
kostja
  • 60,521
  • 48
  • 179
  • 224
23
votes
2 answers

CDI Extension for Flyway

I tried to run flyway in my application before hibernate is hooking in on my JBoss AS 7.1. I tried with an @javax.ejb.Startup annotation, but this gets executed AFTER Hibernate is initialized and the database scheme is checked. So as far as I…
Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45
22
votes
1 answer

@javax.faces.bean.ManagedProperty in CDI @Named bean returns null

I'm trying to deal with @javax.faces.bean.ManagedProperty but without success ! I've been following this guide, and it not seems that hard. But my code simply won't work! Here's a little snippet @ManagedBean @SessionScoped public class LoginBean { …
StepTNT
  • 3,867
  • 7
  • 41
  • 82
22
votes
2 answers

How to inject a non-serializable class (like java.util.ResourceBundle) with Weld

I want to create a Producer that makes it possible to inject a java.util.ResourceBundle into any class in order to get localized Strings easily. My ResourceBundle-Producer looks like this: public class ResourceBundleProducer { @Inject …
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
21
votes
3 answers

Injecting a bean from a different Jar in Weld

I have two Jars A and B where A depends on B. Jar B has a single class: @ApplicationScoped public class MyManagedBean { private String user; public MyManagedBean(){ //Constructor necesary for CDI } @Inject public…
narduk
  • 964
  • 1
  • 9
  • 19
21
votes
3 answers

Java CDI @PersistenceContext and thread safety

Is an EntityManager @Inject[ed] as follows in muliple classes threadsafe? @PersistenceContext(unitName="blah") private EntityManager em; This question and this one seem to be Spring specific. I am using Jave EE CDI services
auser
  • 6,307
  • 13
  • 41
  • 63
21
votes
2 answers

What is the Spring equivalent for CDI's Instance, or Guices Provider

In CDI you can define an object that will give you items of a certain type, using: @Inject Instance myObjectInstance; //... MyObject myObjectInstance.get(); Similarly in Guice you can do: @Inject Provider
bogdan.mustiata
  • 1,725
  • 17
  • 26
20
votes
2 answers

Java EE 6 : @Inject and Instance

I have a question about the @Inject annotation in java ee 6 : What is the difference between : @Inject private TestBean test; @Inject private Instance test2; To have the reference : test2.get(); Some infos about Instance :…
jihedMaster
  • 331
  • 1
  • 3
  • 11
20
votes
2 answers

Are JSF 2.x @ViewScoped managed beans thread safe?

I've been googling for a couple hours on this issue to no eval. WELD docs and the CDI spec are pretty clear regarding thread safety of the scopes provided. For example: Application Scope - not safe Session Scope - not safe Request Scope - safe,…
Mark
  • 2,932
  • 18
  • 15
20
votes
2 answers

JEE7: Do EJB and CDI beans support container-managed transactions?

Java EE7 consists of a bunch of "bean" definitions: Managed Beans 1.0 (JSR-316 / JSR-250) Dependency Injection for Java 1.0 (JSR-330) CDI 1.1 (JSR-346) JSF Managed Beans 2.2 (JSR-344) EJB 3.2 (JSR-345) In order to get rid of the chaos in my mind,…
SputNick
  • 1,231
  • 5
  • 15
  • 26
19
votes
1 answer

Is it possible to @Inject a @RequestScoped bean into a @Stateless EJB?

Is it possible to inject a request-scoped CDI bean into a Stateless session bean? I had asked a related question and thought the specific CDI @RequestScoped into @Stateless question merited its own post. Passing state between EJB methods /…
wrschneider
  • 17,913
  • 16
  • 96
  • 176
19
votes
2 answers

How to inject mocks while testing classes using CDI in production

I am programming in a Java SE environment using WELD-SE for dependency injection. Therefore dependencies of a class look something like this: public class ProductionCodeClass { @Inject private DependencyClass dependency; } When writing a…
Matthias Wimmer
  • 3,789
  • 2
  • 22
  • 41
19
votes
1 answer

Is there an equivalent in CDI(WELD) to build definitions (as done in Guice modules) and then create an Injector?

I like the way Guice makes it fairly straight forward to manually create your own modules each with their own bindings done in code. CDI on the other hand seems to rely more on magic rather than programmatic access to sest bindings. Am i wrong or…
mP.
  • 18,002
  • 10
  • 71
  • 105