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

Any one had Logging interceptor for JSF 2.0 Managed bean working

I have a managed bean with lots of methods. I want to log the entry and exit times of each method invocation . I thought of reusing the Interceptor that works successfully with my EJB's. I am having no luck with it and interceptor isn't getting…
gautirao
  • 130
  • 10
1
vote
0 answers

Apache OpenWebBeans SEContainer throws UnsatisfiedResolutionException

I want to test my classes in an embedded CDI container. For this reason i'm using OpenWebBeans' integration for Junit 5. This is a sample test class: @Cdi(disableDiscovery = true, classes = {Pbkdf2PasswordHashImpl.class}) public class…
cidra
  • 374
  • 1
  • 4
  • 16
1
vote
1 answer

Cannot have inject dynamic instance in quarkus when using @Dependent

I am using a library that is not thread safe and I want a fresh instance of that library object whenever I use it. I made a test repo here : https://github.com/lud/test-quarkus-arc The library comes with two classes, SomeLibraryClass, which I need…
lud
  • 1,941
  • 20
  • 35
1
vote
0 answers

Getting the Bean name is ambiguous in Managed Bean class org.apache.myfaces.push.cdi.PushContextFactoryBean

Hi friends I getting the below error updating enterprise application project from wildfly 18 to wildfly 24, I am using myfaces-2.3.1 version and primeface11.x jar. ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread)…
Rishi Gautam
  • 1,948
  • 3
  • 21
  • 31
1
vote
0 answers

Javax dependency injection error in Wyldfly 10 deployment

I have a problem when I try to deploy my app in Wildfly 10. The problem refer to dependency injection for an interface. My app is a maven multi-module project. The interface and the dependency injection are in the same module. I have already the…
Laso83
  • 11
  • 2
1
vote
1 answer

Interface CDI wildfly external projects

So my idea is to have 3 projects: com.tests.interfaces com.tests.services com.tests.schedulers com.tests.interfaces basically is: @Remote public interface IFoo { public String test(); } com.tests.services is: @Stateless(name = "IFoo") public…
Marllon Nasser
  • 390
  • 4
  • 22
1
vote
1 answer

how to pass a datasource to a library?

I am writing a library which retrieves data from a specific data schema. This library holds a Datasource object which can be anything. Right now I have defined the name of the datasource within the library which I would like to avoid. import…
M4V3N
  • 600
  • 6
  • 21
1
vote
1 answer

Wicket: What to keep in mind when making a (CDI-enabled) Page Serializable?

What do I need to keep in mind when making a Wicket Page Serializable? I'm especially concerned about CDI beans - is it okay to rely on seam-wicket to re-inject beans as necessary? public class ChannelLogPage extends BaseLayoutPage implements…
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
1
vote
3 answers

Fire event with subclass runtime type

I want to fire an event in CDI whose type I can only determine on runtime. For instance, let's say there's some interface A with implementing classes AA and AB. I have two observers: public void observeAA(@Observes AA aa) { } public void…
Artefacto
  • 96,375
  • 17
  • 202
  • 225
1
vote
0 answers

Using CDI feature with vaadin 7/8 and 14

I try to convert a vaadin 8 project (also using vaadin7 compatibility mode) to vaadin 14. The project use lot of EJB and CDI features. Due to the quantity of screen to migrate and because the project is constantly enhanced, we decided to run vaadin…
1
vote
1 answer

WELD-001562: Producer method with Generic signature

I am using weld 4.0 and in one of my producer methods, I have this signature: @Produces @ConfigurationType public T getConfigurationInstance(InjectionPoint injectionPoint){
Venkata Rahul S
  • 304
  • 2
  • 10
1
vote
0 answers

CDI events: Transactional observer called BEFORE_COMPLETION causes INSERT statements to be executed twice

I'm having problems with transactional CDI events. I want to perform some operations at the end of a transaction, before the commit. Currently I'm using transactional CDI events for this. Here ist the code that saves an entity (or gets it, if it's…
matthjes
  • 671
  • 7
  • 20
1
vote
1 answer

How can I add Seam Security to my Netbeans Java EE 6 application?

I'm developing a web application using Java EE 6/JSF2/CDI with NetBeans and I want to use the Security Module of Seam to manage the authorization/authentication process. Is it possible?
SDReyes
  • 9,798
  • 16
  • 53
  • 92
1
vote
2 answers

CDI 2 Event ordering with @Priority not work

The answer of this question describes that it is possible to sort CDI events with the @Priority annotation. Execution order of different CDI Events in same transaction Since CDI 2.0 (check here, Chapter 10.5.2), you may define an order using the…
NF117
  • 13
  • 3
1
vote
1 answer

Jakarta EE CDI Event TransactionPhase.AFTER_SUCCESS event order is reverse

When annotation an observer method with AFTER_SUCCESS, the events are received in the reverse order they actually have been fired during the transaction. Example pseudo code: @Transactional void test(){ pushEvent.fire( new PushEvent(10) ); …
user39950
  • 464
  • 3
  • 15
1 2 3
99
100