Home » Java » Enterprise Java » Lightweight Web Application: PrimeFaces (JSF) + Guice + MyBatis (Part 1)

About Boris Lam

Boris Lam
Boris is an experienced Java developer with in Hong Kong. His expertise is in Java EE technology, object-oriented application development, and the use of open source frameworks (e.g. Spring , Apache MyFaces). In recent years, he is primarily involved in framework development and architectural design in serveral Government related software development projects.

Lightweight Web Application: PrimeFaces (JSF) + Guice + MyBatis (Part 1)

Recently, my friend asks me how to build a lightweight java web application. Many Java web developer would choose Spring and hibernate to build a traditional web application. However, it may not be lightweight enough. I suggested him try to use Guice and MyBatis to build the application framework. Although Spring is more feature-riched than Guice, I admitted that Guice is more lightweight and easier to use. MyBatis is also a lightweight SQL map framework. It can integrate with Guice framework very well.

Here, I will try to set up a simple web application with PrimeFaces, Guice and MyBatis. I hope that my friend could learn how to do it.

Using MyFaces and PrimeFaces in presentation layer.

Integrate MyFaces and PrimeFaces is simple. Just simply get the JARS file from MyFaces website and PrimeFaces website.

For MyFaces, just add the following sample configuration into your web.xml.

    <servlet-name>Faces Servlet</servlet-name>
    <servlet-name>Faces Servlet</servlet-name>

For PrimeFaces, there should be no configuration. If you want to use the PrimeFaces theme, you could add the following context parameter in the web.xml.


Integrate Google Guice with JSF

Google Guice will be used as IOC container. Dependency Injection for service class and SQL mapper class is done though Guice. To integrate with JSF, I suggest to simply add a ServletContextListener to do it.

Add the following configration in your web.xml:


Inside the ServletContextListener, just create a Guice Injector and put it in ServletContext:

public class GuiceContextListener implements ServletContextListener {

   public void contextDestroyed(ServletContextEvent servletContextEvent) {
     ServletContext servletContext = servletContextEvent.getServletContext();

   public void contextInitialized(ServletContextEvent servletContextEvent) {
     Injector injector = Guice.createInjector(
       new MyBatisModule() {
        protected void initialize() {         
         //add singleton service class

     ServletContext servletContext = servletContextEvent.getServletContext();
     servletContext.setAttribute(Injector.class.getName(), injector);

Create a JSF base backing bean.

In the base backing bean, get the Guice injector in the PostConstruct method. Then, every backing beans in your web application need to extends this base backing bean.

package org.borislam.view;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import com.google.inject.Injector;

public abstract class BasePageBean implements Serializable{

   private Injector injector;

   public BasePageBean() {}

   public Injector getInjector() {
     if(injector == null) {
       ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().
       injector = (Injector)servletContext.getAttribute(Injector.class.getName());  
     return injector;
   public void setInjector(Injector injector) {
     this.injector = injector;

   public void init() {

In the second part, I will continue to demonstrate how to integrate MyBatis and Guice.

Reference: Lightweight Web Application Framework : PrimeFaces (JSF) + Guice + MyBatis (PART1) from our JCG partner Boris Lam at the Programming Peacefully blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Email address:

Leave a Reply

Notify of