MyArchiBook

CDI – Lifecycle Management by Container

with 3 comments

We know that, CDI Managed Beans are contextual. Their lifecycle management is done by the container. One of its main advantages is type-safe dependency injection.

But how does it do it?
In the below code, during application initialization process, the container checks for Type-Safe Resolution for News bean annotated with @Inject and then instantiates it.

TYPE-SAFE RESOLUTION:

Here the container checks ,

  • if the bean type is equal to the required type(type: News here) at the injection point in Consumer Bean class.
  • if the qualifier of bean is equal to the required qualifier at the injection point.
  • (If no qualifier specified , then default qualifier is @Default)


@Stateless
public class News {
  public String getLatestNews(){
    return "FIFA 2014 WorldCup";
  }
}

@Stateless
@Path(value = "consumer")
public class Consumer {

  @Inject
  private News news;

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("latestNews")
  public String tweetLatestNews(){
    return news.getLatestNews();
  }
}

Any mismatch will result in unsatisfied or ambiguous dependency problem.

CREATION AND DESTRUCTION BY CONTAINER:

 

Reference:

http://www.oracle.com/technetwork/articles/java/cdi-javaee-bien-225152.html
http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html
http://docs.jboss.org/cdi/spec/1.0/html/contexts.html
http://docs.jboss.org/cdi/spec/1.0/html/injectionelresolution.html#ambigdependencies

Advertisements

3 Responses

Subscribe to comments with RSS.

  1. Nice article.
    You are writing a lot of articles in jee7 angular
    Can you write an article with all of them put together using nosql like mongodb? What is your view on MEAN? I focus on nodejs for REST, have you handled it? I think we should speak on security aspects as well.
    I checked your git sources as well. Code is very simple and clean.

    Good work!

    Veena Tripathi

    July 4, 2015 at 8:56 AM

    • Thank you Veena ! Happy to know that you like my articles.
      Nodejs yes, I have used it to develop quick light-weight code in a few mins.
      My view point on MEAN, I will write-it up as an article because it requires a complete end to end write-up discussion & analysis.
      I am adding your article request to my blog preference list.
      Please keep following.

      thangaveluaishwarya

      July 9, 2015 at 2:03 PM

  2. Any changes needed to make it work on tomee or wild fly? I can’t use glassfish in my production.

    Jagdeesh

    September 16, 2015 at 8:55 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: