Tuesday, May 3, 2016

JSF + Spring Integration, JavaConfig and the EL Resolver

One really great thing in Spring is the ability to configure your application inside the code itself using annotations, specifically with the @Configuration and @Bean annotations. And with the introduction of JSF 2 you no longer need the faces-config.xml for configuration either.  Annotations, yay!

Except that's not entirely true. You do need faces-config.xml when you want to integrate JSF and Spring.

As an experiment I created a JSF managed bean that utilized a Spring bean.  Spring happily created the bean and tried to inject it... but when I tried to run the code the bean was null.  This is because Spring and JSF are stepping all over each other; and this makes sense, because how would they know about one another if you don't tell them?  Fortunately the way to resolve this is to use create a faces-config.xml and add this one simple entry:

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

And that's it.  They will happily cooperate after this.  But there doesn't appear to be any way to do this using JavaConfig :(  My dream of an XML free application will have to wait for now...



No comments:

Post a Comment