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

Is it possible to create a CDI Event factory?

On my quarkus app i have a application scope service with a lot of cdi event injections @ApplicationScoped public class SomeService { @Inject Event eventAEmitter; @Inject Event eventBEmitter; ... …
Bentz
  • 103
  • 7
1
vote
1 answer

Microprofile REST client providers not active if in separate jar file

I'd like to use some Microprofile REST client, injected with @RestClient, in different war files. I need to register a javax.ws.rs.client.ClientRequestFilter for basic authentication. If this filter is in my war project, it works fine (for example…
grigouille
  • 511
  • 3
  • 14
1
vote
0 answers

When CDI speaks of a parameterized type, does it also incorrectly mean array types of parameterized element types?

CDI frequently speaks of parameterized types. Does CDI (incorrectly) consider the array type java.util.List[] to be a parameterized type? (It is not, of course; its element type is.) I ask because there is a part of the section on…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
1
vote
0 answers

My custom "cdi" context is not getting applied/ called

I have a custom @InjectableContext which I register during my build step @BuildStep ContextConfiguratorBuildItem registerContext(ContextRegistrationPhaseBuildItem contextRegistrationPhase) { return new ContextConfiguratorBuildItem( …
Herr Derb
  • 4,977
  • 5
  • 34
  • 62
1
vote
1 answer

@ViewScoped bean behaves like @SessionScoped or @ApplicationScoped

I have a Spring Boot application using JSF/CDI. Backing beans are annotated with @Named @ViewScoped, but while browsing between other pages values still stay on textboxes. Values should've been cleared. In other words, the backing beans unexpectedly…
1
vote
1 answer

How to use SecurityContext in CDI Async Events

We have moved some processing logic to CDI Async Observers however some of the business methods in the observers have some security checks that use securityContext.isCallerInRole(...) to check for specific roles. Since the securityContext is not…
DaveB
  • 2,953
  • 7
  • 38
  • 60
1
vote
0 answers

JSF 2.3 CDI with spring beans

JSF 2.3, CDI, Tomcat 10, Spring bean. I would like to use the latest JSF 2.3 capabilities from Jakarta with spring jdbc. Since JSF is now part of Jakarta and covered under CDI I am struggling how to initialise spring beans and connect it with JSF.…
1
vote
0 answers

jpms + cdi (weld): WELD-ENV-000033: Invalid bean archive scanning result - found multiple results with the same reference

I'm new to jpms and try to setup a simple environment (java 19, graalvm, latest maven) to start a project with jpms + cdi (weld). My problem is: As soon as I create a(n even empty) module-info.java in my project weld produces an exception like below…
r-uu
  • 423
  • 1
  • 4
  • 18
1
vote
1 answer

Referencing CDI producer method result in h:selectOneMenu

I have a named session scoped bean CustomerRegistration which has a named producer method getNewCustomer which returns a Customer object. There is also CustomerListProducer class which produces all customers as list from the database. On the…
user953217
  • 231
  • 1
  • 10
1
vote
1 answer

How to Inject an Interface which is in the context of SPI

I have a Interface Hello.java which is implemented by 2 classes HelloImpl1.java and HelloImpl2.java I have declared HelloImpl1.java as part of META-INF/services. Now, when I try to I Inject Hello class, it's failing saying ambiguity due to 2…
1
vote
1 answer

Inject @RequestScoped inside @Stateless

I am trying to inject a client depended object inside a statless object. I did manage to do it, but I am not really sure how and why it works because I don't fully understand JavaEE dependency injection . I have 3 relevant…
Pero122
  • 892
  • 11
  • 25
1
vote
0 answers

How to pass parameters to instantiation of a CDI @Dependent bean?

How to pass runtime parameters to the instantiation of a @Dependent bean, obtained by Instance.get() or equivalent in CDI? For instance, consider the code: @Inject Instance instance; ... var name = "Some One"; var bean =…
Julio Faerman
  • 13,228
  • 9
  • 57
  • 75
1
vote
1 answer

Quarkus - @Inject Logger causes NPE in test while using Mockito extension; @QuarkusTest works fine

org.jboss.logging.Logger with @Inject annotation in my code causes NullPointerException in my unit test annotated with @ExtendWith(MockitoExtension.class), if I define it as a org.mockito.@Mock. It's fine if it's used in @QuarkusTest as @Inject…
WesternGun
  • 11,303
  • 6
  • 88
  • 157
1
vote
1 answer

Passing in parameters with constructor injection

I am working on an app where an endpoint is being called to add a user. This calls a service class (that has been injected into the controller class) to add the user. In the service class I have created a custom module that deals with connecting to…
Dan
  • 2,020
  • 5
  • 32
  • 55
1
vote
0 answers

Why does TomEE Plus throw a ' Can't create resource null java.lang.NullPointerException ' during start-up of a JSF 2.0 webapp?

After failing to migrate an old MyFaces 2.0 webapp (app1) from WebLogic Server 12.1.3 to Tomcat Tomcat 9 using OpenWebBeans 2.0.27 due to a potential CDI issue, I was asked to switch my attention back to TomEE Plus since this webapp also makes use…
UserB
  • 25
  • 5