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
19
votes
7 answers

wildfly: reading properties from configuration directory

I'm trying to read deployment specific information from a properties file in my wildfly configuration folder. I tried this: @Singleton @Startup public class DeploymentConfiguration { protected Properties props; @PostConstruct public void…
EasterBunnyBugSmasher
  • 1,507
  • 2
  • 15
  • 34
19
votes
5 answers

Unsatisfied dependencies for type [...] with qualifiers [@Default] at injection point (using @Stateful EJB with CDI)

I have the following code to manage two kinds of repositories. Both repository classes inherit an interface to allow reinitialization of their resources. public interface CachingRepository { public void invalidateCache(); } Global,…
Kawu
  • 13,647
  • 34
  • 123
  • 195
18
votes
3 answers

Can I (and how) lookup CDI managed beans using javax.naming.Context#lookup in EJB module?

Can I (and if so, how?) lookup CDI managed beans using javax.naming.Context#lookup in EJB module? I'm using GlassFish v3. I suppose that I can use @Named, but what is JNDI name of CDI managed bean? I want to lookup them from unmanaged POJOs so I…
zacheusz
  • 8,750
  • 3
  • 36
  • 60
18
votes
6 answers

CDI: Using Interceptors across different modules / bean archives

My Java EE 6 application consists of a war and an ejb module packaged in ear file. I'm using CDI for DI (i.e. I have a beans.xml file in both modules). I want to use a logging interceptor that is defined in the ejb module in the war module, as well.…
Theo
  • 3,074
  • 7
  • 39
  • 54
18
votes
1 answer

Unsatisfied dependencies for type X with qualifiers @Default

I'm trying to inject an object of a given type (Greeter) on an EJB running inside Wildfly 8.2. However, the deployment always fails with the message Unsatisfied dependencies for type Greeter with qualifiers @Default I tried to annotate both the…
Martin
  • 1,317
  • 3
  • 13
  • 18
18
votes
2 answers

CDI on Tomcat 7 - does it make sense?

If I can choose, I use JBoss 7 for a Java EE 6 project using JSF 2 and CDI. But sometimes, the environment for a customer's project is more or less set - so in one case we are limited to Tomcat (6 or maybe 7). So, I read a couple of articles about…
Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96
17
votes
2 answers

Can I use CDI constructor injection for EJBs?

I want to do something like this: @Stateless public class GreeterEjb { private final Greeter greeter; @Inject public GreeterEjb(Greeter greeter) { this.greeter = greeter; } public String greet() { return…
Arend v. Reinersdorff
  • 4,110
  • 2
  • 36
  • 40
17
votes
2 answers

CDI Application and Dependent scopes can conspire to impact garbage collection?

We're starting to experiment with implementing our backend services using CDI. The scenario is this: EJB with @Startup is started when EAR deployed. An ApplicationScoped bean is injected onto this: @ApplicationScoped public class JobPlatform { …
Ben Kirby
  • 904
  • 2
  • 11
  • 29
17
votes
2 answers

Are CDI.current().select().get() and BeanManager.getReference() functionally equivalent?

In the context of JEE/CDI, I find myself typically using a CDI static function when I need to retrieve a CDI managed bean statically from a method. For instance: MyBean myBean = CDI.current().select( MyBean.class ).get() However, from what I can…
Eric B.
  • 23,425
  • 50
  • 169
  • 316
17
votes
2 answers

Why do I need a no-args constructor to use ApplicationScoped beans with Constructor injection within CDI?

I'm trying to apply the constructor injection pattern to beans in my CDI application and am encountering the following error message: 15:18:11,852 ERROR [izone.adams.webapp.error.IzoneExceptionHandler] (default task-40)…
Eric B.
  • 23,425
  • 50
  • 169
  • 316
17
votes
2 answers

Pass Parameter to Instance of @Inject Bean

I am using CDI as injection framework, but I have found some limitations in its usage, and this is one of them. I am trying to initialize the creation of a bean instance with runtime values. Example: @RequestScoped public class MyNumber { int…
Joe Almore
  • 4,036
  • 9
  • 52
  • 77
17
votes
4 answers

Manually Register Class in CDI Container

I have a group of classes which are instantiated by reflection, hence these are not managed by the CDI container, and no injections are made by the context. My question is, is there any way to register these classes in the CDI context, so the…
Joe Almore
  • 4,036
  • 9
  • 52
  • 77
17
votes
4 answers

CDI Ambiguous dependencies

I have a @SessionScoped @Named bean with a @Producer method for a user object: @Named @SessionScoped public class UserBean implements Serializable { //... @Named @Produces @LoggedIn @SessionScoped public MyUser getCurrentUser() {return…
Thor
  • 6,607
  • 13
  • 62
  • 96
16
votes
2 answers

What is the Spring DI equivalent of CDI's InjectionPoint?

I would like to create a Spring's bean producer method which is aware who invoked it, so I've started with the following code: @Configuration public class LoggerProvider { @Bean @Scope("prototype") public Logger produceLogger() { …
Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
16
votes
1 answer

Are Java EE 6 CDI events transactional?

Are Java EE 6 CDI events transactional? If I fire an event in a transaction, and subsequently roll back the transaction, are the effects of the Event listener rolled back as well? Is this behaviour dependent on the event listener itself supporting…
Brian Leathem
  • 4,609
  • 1
  • 24
  • 44