Extreme Java When vanilla Java is not enough


Using Spring with Mojarra InjectionProvider

Are you using SpringBeanFacesELResolver to handle dependency injection on managed beans? It works nice, but your faces-config grows fast with this approach.

Two months ago, I posted about using Spring injection outside application context. I forgot to mention that the same trick can be used to replace ELResolver solution if you are using Mojarra as your JSF provider.

You only need to add a servlet context parameter:


And the implementation of SpringJSFInjectionProvider is as simple as:

public class SpringJSFInjectionProvider
       implements InjectionProvider {
  private WebContainerInjectionProvider wcip =
      new WebContainerInjectionProvider();
  public void inject(Object bean)
         throws InjectionProviderException {
  public void invokePreDestroy(Object bean)
         throws InjectionProviderException {
  public void invokePostConstruct(Object bean)
         throws InjectionProviderException {

Adding this, you can shrink your faces-context, removing all "managed-property" tags. Your managed beans can look more JavaEE 5 after you change this:

private BO bo;
public void setBo(BO bo) {
  this.bo = bo;

To this:

private BO bo;

No setter, no ELResolver. This means less code to maintain! If you plan to migrate from Spring to EJB, this means you will only need to promote the BO to an stateless bean and change @Resource to @EJB!

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment



No trackbacks yet.