Adam Bien's Weblog

Tuesday Apr 12, 2011

Contexts and Dependency Injection in Java EE 6 - Free Article

Contexts and Dependency Injection in Java EE 6 article is online. I get many questions regarding the differences between EJB 3.X and CDI especially regarding the Contexts (the C in CDI). The EJB "vs." CDI issue became the focus of this article. Feedback, as always, is highly appreciated!

Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns


Hi Adam, not sure if I get it right.

Can the EJBs be annotated with CDI annotations for example @Qualifier - to resolve required implementation of an interface?

Is there any difference in an EJB injecting another EJB using @EJB and @Inject?


public class EJB1 {
@EJB private EJB2 ref2a;
@Inject private EJB2 ref2b; //is this equivalent as ref2a?

//is this possible?
@EJB @Ingredient(A) Ingredient ejb3a;
@EJB @Ingredient(B) Ingredient ejb3b;
@Inject @Any Instance<Ingredient> ingredients;


public class EJB2 {

@Stateless @Ingredient(A)
public class EJB3a implements Ingredient {
@Stateless @Ingredient(B)
public class EJB3b implements Ingredient {

Thanks for article.

Posted by cody on April 16, 2011 at 11:08 PM CEST #

Adam: excellent article. Not to give you a big head, but many more of your thoughts need to be published on or!

I have a question though... Is there any sort of JEE provided api for AOP(Interceptors) JSF managed beans?

In JSF, I'd like to be able to use "@EJB SomeService serviceHandle", but wrap it with a special security, tracking, or other interceptor.

Posted by Jon on April 22, 2011 at 06:46 PM CEST #

Hi Adam, thanks to your excellent book I'm progressively rethinking most of my outdated JEE best practices. I would have some clue on how to overcome a problem I'm facing with the dynamic configuration strategy that you describe in your book. What happens in my testbed (WebSphere 8.5.5) is that when I inject Instance<String> the producer method, that is supposed to return the proper configuration setting, is not able to do its work because it receives a null InjectionPoint. I tried annotating the consumer bean as simple ManagedBean and Stateless session bean but nothing changed, the ip is always null. Any idea or suggestion? Thanks again for all I'm learning from your writings

Posted by Patrizio on August 30, 2013 at 03:32 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments