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
0 answers

Quarkus framework provides some annotation for inject a bean depend of the property value?

Hi guys I have a doubt I am trying to inject or only initialize filter request only when a property value is enabled. When the value es false this filter not will be inject o intercept requests. For example in Spring Framework you ca use…
1
vote
1 answer

How does RequestScope in Quarkus/CDI work?

I did some experimentation with Quarkus and I am having difficulties understanding how @RequestScoped works. Coming from Spring, I would be expecting that the following code should not work and throw an Exception: @ApplicationScoped public class…
Dave
  • 420
  • 1
  • 3
  • 14
1
vote
0 answers

Arquillian : Dependency Injection not working

I am trying to use Arquillian to run tests on a remote Wildfly server. I am not sure why the Dependency Injection does not work. Here is the relevant section from POM: org.jboss.arquillian.junit
userx
  • 1,083
  • 5
  • 18
  • 36
1
vote
0 answers

Java CDI: @Decorator gets recursively called

Problem I'm currently building an application using Quarkus (2.9.2.Final). The project contains multiple modules that depend on each other. The usecase module contains the interfaces for the repositories of the repository module. I now want to…
1
vote
1 answer

IllegalStateException in conditional observer in Vaadin @RouteScoped component

We are using Vaadin 23 (23.0.9) with vaadin-cdi (14.0.0) and are having problems with conditional observers in @RouteScoped components (like it is described in the tutorial): @RouteScoped public class ScopedComponent extends Div { private void…
nluckas
  • 21
  • 3
1
vote
1 answer

Problem when deploying PF 11 app with CDI (weld) on Tomee Plume 8.0.9

I'm trying to migrate a PF6 app to PF11, and, after deploying it on tomee 8.0.9, I've got the following error: Caused by: org.apache.catalina.LifecycleException: Failed to start component…
Dan Chirita
  • 119
  • 9
1
vote
3 answers

Why is Glassfish 3.1.1 unable to create my Stateless Session Bean?

Glassfish 3.1.1 (build 12) Application deployed as a WAR using JAX-RS, EJB3, JPA There are no deployment errors in the logs. This is a very clean glassfish 3.1.1 install, with only this application deployed. This application works in Glassfish…
Kyle Renfro
  • 1,238
  • 2
  • 12
  • 18
1
vote
1 answer

Problem using quarkus and io.vertx.mutiny.mysqlclient.MySQLPool

I have problem using quarkus using in persistence layer io.vertx.mutiny.mysqlclient.MySQLPool This is the repo class: @ApplicationScoped public class OrderDaoImpl implements OrderDao { @Inject MySQLPool client; @Override public…
krz00
  • 11
  • 2
1
vote
1 answer

Glassfish Jersey + Weld creates AbstractMethodError

I have a web-application based on JDK8, Tomcat7 (V2.1), Jersey (2.35) and CDI-Weld (2.4.8.Final) Since I am starting tht web container, I receive this exception always twice. These two exceptions will appear then every 5 minutes. I think that any…
Alex
  • 161
  • 1
  • 13
1
vote
1 answer

EJB update bean property at runtime

I have a bean that contains some web client implementation that can be REST or SOAP: @Stateless public class Service{ private Client client; @PostConstruct public void init() { this.client = new RESTClient(); } } Is there a way that I…
bxacosta
  • 49
  • 1
  • 5
1
vote
1 answer

Resuable CDI-Beans in Unit-Tests

I have an JavaEE-Application and want to use CDI in the unit-test. Currently i use org.apache.openejb.junit5.RunWithApplicationComposer and my unit tests look like this: @RunWithApplicationComposer(mode = ExtensionMode.PER_EACH) @Classes(cdi = true,…
1
vote
1 answer

Is bean discovery for @QuarkusTest configurable?

Consider the following Quarkus application: @ApplicationScoped @Path("/hello") public class HelloService { @Inject Helper helper; @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello(){ return "Hello…
Johannes Hahn
  • 363
  • 3
  • 18
1
vote
1 answer

Factory using CDI returning capture of type instead of concrete type

I want to create a processor factory that uses CDI to get all available processors. The factory should select the desired processor based on some parameter. So I have my parameter: public abstract class Parameter { } @CorrespondingProcessor(type =…
Martin
  • 852
  • 7
  • 20
1
vote
0 answers

CDI - inject a bean from dependency into class in module

I have the following UserService bean, part of module A: @Stateless @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public class UserService { ... } I want to programatically inject UserService in another class named LoginHandler.…
j.a
  • 31
  • 5
1
vote
1 answer

Why is InvocationInterceptor active in dev-mode?

Today, while profiling a Quarkus app, I found out that io.quarkus.arc.runtime.devconsole.InvocationInterceptor seems to intercept (almost?) all bean classes when Quarkus is running in dev mode, even though the Interceptor has an InterceptorBinding…
Johannes Hahn
  • 363
  • 3
  • 18
1 2 3
99
100