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
28
votes
4 answers

Getting a reference to EntityManager in Java EE applications using CDI

I'm using Java EE 7. I would like to know what is the proper way to inject a JPA EntityManager into an application scoped CDI bean. You can't just inject it using @PersistanceContext annotation, because EntityManager instances are not thread safe.…
Flying Dumpling
  • 1,294
  • 1
  • 11
  • 13
28
votes
2 answers

@ApplicationScoped CDI bean and @PersistenceContext - is this safe?

Is it safe to do something like this with CDI? @Named @ApplicationScoped public class DAO { @PersistenceContext private EntityManager entityManager; } I understand that EntityManager itself is not thread-safe, and therefore should not be…
wrschneider
  • 17,913
  • 16
  • 96
  • 176
28
votes
2 answers

What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?

I've always used the following pattern to construct (SLF4J) loggers: private static final Logger log = LoggerFactory.getLogger(MyClass.class); This has worked so far, but I was wondering about the static context at some point and the need to pass…
Kawu
  • 13,647
  • 34
  • 123
  • 195
27
votes
1 answer

Which is the Spring equivalent for the CDI @Produces annotation?

When I was working with CDI, I could use the @Produces annotation to create a producer method to be called to choose which bean that implemented an interface would be injected by the @Inject annotation . Now I am working with Spring, but I didn't…
Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199
27
votes
5 answers

How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1

I'm currently evaluating Java EE 6 / JSF 2.1 with RichFaces. A bean which is declared as @ManagedBean @ViewScoped Gets an ID set (to prepare e.g. a delete operation). Via JSF a confirmation popup is displayed. If the user confirms, the delete…
stacker
  • 68,052
  • 28
  • 140
  • 210
27
votes
1 answer

What is the default scope of a Named CDI bean?

Is there any default scope for a @Named CDI bean without additional @...Scoped annotations? I have not found any relevant information in the official Weld documentation. A @Named bean can be accessed over JSF without additional annotations, so some…
kostja
  • 60,521
  • 48
  • 179
  • 224
26
votes
1 answer

Bean instance of a shorter scope injected in a bean instance of a larger scope in CDI - how does it work?

Consider the following request-scoped CDI bean: @RequestScoped public class RequestScopedBean { // ... } Now, I inject it in a application-scoped bean: @ApplicationScoped public class ApplicationScopedBean { @Inject private…
brandizzi
  • 26,083
  • 8
  • 103
  • 158
26
votes
3 answers

How to create a modular JSF 2.0 application?

I have an application with a well defined interface. It uses CDI for resolution of the modules, (Specifically it uses Instance<> injection points on API interfaces to resolve modules) and passes various data back and fourth via the interfaces…
SplinterReality
  • 3,400
  • 1
  • 23
  • 45
26
votes
1 answer

JEE6 @ApplicationScoped bean and concurrency

I need to write a bean that would act as a counter of how many times it was accessed. I'm thinking of using @ApplicationScoped bean with AtomicInteger like that @ApplicationScoped class VisitsCounter { private AtomicInteger counter; …
grafthez
  • 3,921
  • 4
  • 28
  • 42
25
votes
4 answers

Constructor injection vs Field injection

When injecting any services, I have two choices : Field injection: @Inject private MyService myService; or Constructor injection: private MyService myService; @Inject public ClassWhereIWantToInject(MyService mySerivce){ this.myService =…
Riadh
  • 1,088
  • 2
  • 12
  • 25
25
votes
4 answers

What is difference between resource injection and dependency injection (CDI) in Java?

I have been learning Java EE for while and found Java EE provides two types of injection mechanisms Resource Injection Dependency Injection Please guide me to understand the difference between Resource Injection & Dependency Injection.
user4867774
25
votes
5 answers

What is a CDI bean?

I am a little bit confused, we call CDI bean to the beans which we inject them using @Inject annotation or the beans which we use @Inject inside them ?
Yashar
  • 1,122
  • 2
  • 15
  • 43
25
votes
2 answers

Why different persistence units with separated data sources query the same data source?

I'm developing a webapp which needs access to two different database servers (H2 and Oracle). The container is an Apache Tomee 1.5.1 and I'm using the Java EE stack with libraries provided in it (JSF, JPA, CDI, EJB, etc.). I'm trying to use two…
Alberto Segura
  • 755
  • 1
  • 12
  • 28
25
votes
6 answers

How to use CDI for method parameter injection?

Is it possible to use CDI to inject parameters into method calls? The expected behaviour would be similar to field injection. The preferred producer is looked up and the product is used. What I would like to do is this: public void foo(@Inject Bar…
kostja
  • 60,521
  • 48
  • 179
  • 224
24
votes
4 answers

How to manage EntityManager life-cycle in CDI environment (using Tomcat)

I am developing one application and I have started to use CDI along with JSF and JPA. The web container is Tomcat. I am very confused about EntityManager life-cycle in my CDI beans and I would need a good advise to clear some stuff in my…
Ioan
  • 5,152
  • 3
  • 31
  • 50