Integrate Spring into JSF 2

There are many ways to integrate the Spring Framework into a JSF application. In this post I write about a tight integration in which almost all configuration is done with Spring annotations without loosing JSF bean handling in the front end. I have created an application template to show this approach. The project is hosted on GitHub (spring-jsf). In the end of the article you find a guide how to build and run this application.


The main piece of integration of Spring into JSF is done in JSF configuration. We want to use Spring managed classes as JSF beans. To achieve this you have to set org.springframework.web.jsf.el.SpringBeanFacesELResolver as a el-resolver in your JSF configuration.


<?xml version='1.0' encoding='UTF-8'?>	
  xsi:schemaLocation=""	 	xsi:schemaLocation="" version="2.0">	 

You can now configure a Spring class as a JSF managed bean by annotations:

public class UserBean implements Serializable

In this example class UserBean is configured as session scoped JSF Bean with name user. Use it in your templates as usual:

<h:outputLabel value="User: #{user.user.login}" rendered="#{user.ok}"/>


Lets have a lock a the Spring configuration file. What you see has nothing to do with JSF. It is a standard file if you use annotations as much as possible:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" 
  <context:component-scan base-package=""/>
    <property name="location">
  <bean id="dataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="username" value="${jdbc.username}"/>
  <bean id="sessionFactory">
    <property name="dataSource">
      <ref bean="dataSource"/>
    <property name="hibernateProperties">
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
    <property name="annotatedClasses">
  <bean id="transactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  • Line 10: Bean configuration by annotations is activated
  • 11: Classes from package are scanned for annotations.
  • 12-22: Database configuration. At runtime you will need a file in the same folder to set properties jdbc.driverClassName, jdbc.url, jdbc.username and jdbc.password.
  • 23-41: Hibernate configuration
  • 42: Transaction configuration by annotations is activated


In the application template you find one entity class and one dao to handle the user login. The entity class is configured by annotations. The dao is a kind of „POJO-Dao“. Unlike many other daos in Spring application it does not extend or use any Spring class. Instead of this, the Hibernate SessionFactory is injected and you do all database operations with a pure Hibernate session. However, this is not the focus of this article.

Build and run

I created a application template to show how all the stuff works in practice. The application provides a register and login form. Login uses a save hashing function described in this guide from the OWASP Foundation. Fell free to use my application template to start with your own application, fork it on GitHub!

Short guide to build and run my application template:

Clone Git repository

git clone
cd spring-jsf

Create database

mysql -u <USER> -p
create database springjsf;
mysql -u <USER> -p springjsf < src/main/sql/spring-jsf-schema.sql

Set database properties
To set the database properties edit the file src/main/resources/

Maven build

cd spring-jsf
mvn package

Maven compiles the code and creates the War file target/spring-jsf.war. Copy the War file to your Tomcat wepapps directory. Start Tomcat and load

    • Amine
    • 28. November 2013

    Thank’s man

  1. You may also want to read my article Among other things, it mentions @ViewScope (which is pretty problematic because it doesn’t match any Spring scope), and it has a couple of links to simple Spring integration projects on GitHub.

  1. 26. Oktober 2013
    Trackback from : JSF and Spring | hfontis

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: