- 2 minutes read

A couple of weeks ago I wrote a short note mentioning you can use CDI and JSF in Liferay portlets at last.

The good news

And it's true! It's hard to believe for someone like me who has been waiting two years for it. But every CDI core feature my team and I tried works flawlessly in the Liferay portal server. In particular, my team tested

  • basic dependency injection (@Inject, roughly comparable to Spring's @AutoWire)
  • dependency injection with qualifiers (@Inject @Red Color redTrafficLight;)
  • producer methods (@Inject @UserID String userID instead of @Inject User user; String userUD = user.getUserID();)
  • CDI's scopes
  • Interceptors (@AroundInvoke - a simple AOP framework)

There's nothing like a free lunch

The nice thing about CDI is that the specification limits the core CDI functionality to the absolutely necessary minimum. Even JSF's ViewScope is omitted. If you need something like that, CDI makes it very simple to write an extension. For instance, Apache CODI is a great companion framework to CDI. Unfortunately, it doesn't work with the CDI-Portlet-Bridge yet. In other words: if you want to use both CDI and JSF in your Liferay portlet, you have to find a surrogate to @ViewScoped. And of course, you can't use CODI's other improvements (have a look at the CDI landscape presentation to get an idea what CODI gives you).

How to get it up and running

The simplest way to get your CDI & JSF portlet up and running is to play with Neil Griffins CDI Demo Portlet. The basic idea to use it is to include a jar file (cdi1-portlet-bridge-shared.jar) and to add a few lines to your project's web.xml:

CDIPortletFilter com.liferay.cdi.portlet.bridge.CDIPortletFilter ACTION_PHASE RENDER_PHASE RESOURCE_PHASE CDIPortletFilter 1

That's all it needs! The simplicity is almost a disappointment. Mind you, we had to wait at least two year for the solution, it can't be that easy :).


Also read:

The JIRA ticket on CDI, Liferay and portlets


Comments