Skip to main content

Learning CDI (Contexts and Dependency Injection) Part 2

This is second part of CDI discussion. If u r not visited, Please visit first part,

Qualifier
           Its annotation, its helps to create our qualifier annotation. We already seen if we have more than one implementation of particular interface then we can't inject, because ambiguity issues are arise. so we need to define own qualifiers and marked the annotation with that qualifier. When inject we need to mention the qualifier.


For Example

We have one interface and two implementation

Hello Interface



HelloImplOne Implementation




HelloImplTwo Implementation


 In Injection  Point (i.e where u inject the class, i.e where u use @inject key word) if u try to inject using interface like this u get AmbiguousResolutionException.


but we can still use like this
@javax.inject.Inject HelloImplOne hello;
@javax.inject.Inject HelloImplTwo hello;


so we need use qualifier

we can create own qulaifier like any class





here
@Qualifier - is annotation to represent that we r creating custom qualifier. while deploying the container
check that class contain qualifier annotation is present. If its present container treat  class as qualifier.

@Retention(RUNTIME) - its like decision maker. this annotation decides that our annotation is present in runtime or compile time.
possible Values are (RUNTIME, CLASS, SOURCE)
RUNTIME - annotation is stored in class file and while running JVM can see the annotation is reflection API
CLASS - annotaion is stored in class file but its not visible in JVM. Its mainly used by deployment time (ie. we can inform to container)
SOURCE - this annotation is not stored in class file. its only used for inform some info to compiler. (Ex: @Override its inform to compiler this method is going to override.).

@Target({METHOD, FIELD, PARAMETER, TYPE}) - What are the places we can use our annotation. here we can use to any method, fields, parameter declaration's and any class and interface see all possible values hereso finally we create 'Hi' qualifier from above code. we use this qualifier to solve the ambiguous problem.take any class implementation from above code (here i take HelloImplTwo) and add our new qualifier 'Hi' with '@' symbol(it represent annotation)

HelloImplTwo Implementation



thats its. now in injection point
if you using like this
then Class HelloImplOne is injected.

if you using like this

then Class HelloImplTwo is injected.


Please see the Third part here


see the Screen cast 




Please see the Third part here


Comments are welcomed

Popular posts from this blog

Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

In this post i will show Tomcat Clustering in Docker Container. In  my previous post i discussed how to achieve tomcat clustering with Nginx Front end . Its almost same scenario, but this time we will achieve via docker container. Docker Docker  is an  open-source  project that automates the deployment of  applications  inside  software containers , by providing an additional layer of abstraction and automation of  operating-system-level virtualization  on  Linux . [4]  Docker uses resource isolation features of the Linux kernel  such as  cgroups  and kernel  namespaces  to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining  virtual machine   --Wikipedia

Understanding Virtual Host Concept in Tomcat

Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective. Scenario: I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com The following diagram is my outline. Outline structure of Virtual Host Concept in Tomcat Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address. How all domain names are pointing to my Tomcat?                   When we purchase the domain name we need to update the our tomcat IP address to it. like or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts Now How Setup...

File Upload is Easy in JSF2.2

To bring the File Upload feature in Java based web application is one of the difficult and complex job, we need to dependent on 3rd party libraries like Apache Commons FileUpload  Libraries. These libraries codes are complex and most of them boilerplate code. If we are using Java Server Faces (JSF), we have the page with some fields and file upload menu the its add more complexity, fields are binded to backing bean but these file uploads components are need to tie up with some 3rd party file upload libraries. In Primefaces provide easy way to do the file upload in JSF web application, even though primefaces internally used the same Apache Commons FileUpload Libraries, but provide simple JSF tags. We need configure  some listeners.