Saturday, November 19, 2011

CDI (JSR 299) with JBoss WELD


Here I will describe about the implementation of JSR 299, JBOSS WELD, by using this we can achieve context dependency injection (akka CDI). For JSR 299 till now there are two main implementations are available.

  1. Apache WebBeans
  2. JBoss WELD
I will show a demo of CDI with JBoss Weld in a simple web application. JBoss Weld has easier implementation when it compared with Apache WebBean. Tools used are JKD7, Tomcat7, Weld API, Netbeans IDE.

Below JBoss Weld jar files are required.
  1. weld-api.jar
  2. weld-build-config.jar
  3. weld-core.jar
  4. weld-servlet.jar
  5. weld-sp.jar
These jar files can be downloaded at weld site.
Now create a simple Web Project in NetBeans IDE.
The project will look like this

Add the above jar files into WEB-INF/lib folder.For CDI we have to create an empty beans.xml file. We have place this is WEB-INF folder, parallel to web.xml file.

Now create a package like com.weld.beans in the source folder. Create a class named GreetService & add a method greet() into it. The class/method signature is given below.

public class GreetService {

public String greet (final String name){
return "Hallo "+ name + " from WELD";
}

}

This is simple java class with a simple method. we are going to inject the class in run-time & invoke the method. To do so we will create a servlet & inject GreetService class. Create servlet named CDIServlet in the package com.weld.servlets. The default created servlet will look like below.

@WebServlet(name = "CDIServlet", urlPatterns = {"/CDIServlet"})
public class CDIServlet extends HttpServlet {
/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

out.println("

Servlet CDIServlet

");

} finally {
out.close();
}
}

//
/**
* Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}

Now we will inject with an annotation @Inject (javax.inject.Inject part of CDI).

Add the below code at the top level of the servlet.

private @Inject GreetService greetService;

the @Inject will create a injection point for the class GreetService at this peace of the servlet code.

To test this let's print the GreetService object reference. to do so add the below code after processRequest method of CDI Servlet.

LOGGER.log(Level.INFO,"GreetService Object is {0}", greetService);

To invoke the greet method of the GreetService class update the processRequest method with beow code.

out.println("Servlet CDIServlet at " + greetService.greet("abani") );

Save the servlet. Now we have to test the CDI implementation. We can run the servlet & watch the servler log. To do so right click on the servlet & choose run file option in menu.
after successfully invoking the servlet we can see some logs as below.

Nov 19, 2011 8:28:37 PM com.weld.servlets.CDIServlet processRequest
INFO: GreetService Object is com.weld.beans.GreetService@68a427

The servlet output will look like below.





No comments:

Post a Comment