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
1
vote
1 answer

Design decision for a Java EE 6 (EJB, JSF, CDI, JPA) application

I am developing a small (but growing) Java EE project based on the technologies EJB 3.1, JSF 2, CDI (WELD) and JPA 2 deployed on a JBOSS AS 7.1Beta1. As a starting point I created a Maven project based on the Knappsack Maven archetypes. My…
Kai
  • 207
  • 4
  • 10
1
vote
2 answers

@Inject in base class is null in base class methods, ok in derived class

In the following code I'm trying to inject a SessionScoped bean into a stateless EJB, but I want to do the @Inject in an abstract base class of the EJB. According to the CDI spec it seems to suggest that this should work (never a spec to waste…
Oversteer
  • 1,778
  • 1
  • 22
  • 38
1
vote
2 answers

Use @EJB as injection annotation in Weld

I have an application which is part JavaEE (the server side) part JavaSE (the client side). As I want that client to be well architectured, I use Weld in it to inject various components. Some of these components should be server-side @EJB. What I…
Riduidel
  • 22,052
  • 14
  • 85
  • 185
1
vote
1 answer

Tomcat OpenEJB and CDI

I am using OpenEJB 3.1 embedded in Tomcat6.33 for testing our EJBs. Now I am trying to use CDI in EJB. I have done the settings for using CDI in Tomcat so now I can use @Inject in my Servlet and everything works fine. However when I try to use…
user667022
  • 351
  • 3
  • 13
1
vote
0 answers

CDI SessionScoped Bean instance remains unchanged when login with different user

I've been looking for the workaround of this problem for rather plenty of time and no result, so I ask question here. Simply speaking, I'm using a CDI SessionScoped Bean User in my project to manage user information and display them on jsf pages.…
Jason Yang
  • 115
  • 1
  • 5
1
vote
1 answer

CDI exception when trying to use Infinispan JCache on OpenLiberty

I'm trying to use Infinispan and JCache API to add some caching feature tu my JEE app running on OpenLiberty. I'm trying to make it work in a POC project so, for now, I don't have any code, I'm just deploying a war with required dependencies on the…
Clément Honoré
  • 137
  • 1
  • 12
1
vote
1 answer

No available endpoints in a helidon-mp 4.0.0-M1 application

I am experimenting with 4.0.0-M1 and helidon-mp. I have a simple app, with a database connection. However I am using CDI and am wondering why no endpoints are exposed. All my endpoints on localhost:8080 yield a 404 (e.g. /health, /metrics,…
1
vote
0 answers

How to overcome multiple @Produces methods?

I have javaEE application in which i have factory class which has multiple @Produces methods. This method produce classes that i use in application with @Inject annotation. The issue is, when i want to add new @Produce method i need to write it…
1
vote
1 answer

Does any class from a Spring ecosystem implement SeContainerInitializer or SeContainer?

I'm trying to use Jakarta jnosql Artemis along with a Spring Boot application. Artemis requires a CDI (Context and Dependency Injection) implementation to be provided. Overall, Spring Framework does it, however it implements it's own, similar…
xinus01
  • 47
  • 1
  • 8
1
vote
1 answer

Changing reference of ViewScoped beans in JSF 2.3 dynamically

I am working on an application in JSF, in which I have two ViewScoped beans within the same screen, and they need to communicate with each other. The first bean represents the main screen, and the other bean is a popup window where the user selects…
dssof
  • 117
  • 6
1
vote
0 answers

What scope should be used for ResteasyClient?

I am a bit confused about the proper scope of ResteasyClient in Quarkus. Based on the Quarkus documentation, the default scope for ResteasyClient is @Dependent which is bound to the bean injecting it, as in this explain. What I understand about it…
khesam109
  • 510
  • 9
  • 16
1
vote
2 answers

Spring 3.0 Disable @Inject Annotation Processing

Is there a way to disable the @Inject annotation processing of spring 3.0? I'm trying to use the CDI @Conversation Scope together with spring, but when it comes to @Inject private Conversation conversation; spring tries to autowire the…
flash
  • 6,730
  • 7
  • 46
  • 70
1
vote
3 answers

CDI producer that handles any qualifier

Can I create a producer method that handles any arbitrary qualifier? i.e.: even qualifiers I'm not "aware-of" at design time? The challenge is that I want to create an injectable simplification of an outside-developed injectable component. The…
Glenn Lane
  • 3,892
  • 17
  • 31
1
vote
1 answer

CDI Extensions: Can I expose one interface in two scopes?

I have an interface in a unit testing framework: CDIMocker. I'm currently using an interceptor to allow mocking in a CDI container. It's an experiment - one of a couple of approaches to unit testing I'm considering. (The other main contender is to…
Richard Corfield
  • 717
  • 6
  • 19
1
vote
1 answer

does this JSF pattern break dependency injection?

I have a JSF2 project (Mojarra on GlassFish 3.1). I have a ViewScoped bean that references services through a utility class like so: @ManagedBean @ApplicationScoped public static class ServicesUtil { @EJB UserService userService; @EJB …
user550738