JSF 2 + Spring 3 Integration Example
In this tutorial, we will show you how to integrate JSF 2.0 with Spring 3 using :
- JSF XML faces-config.xml
- Spring annotations
- JSR-330 standard injection
Tools and technologies used :
- JSF 2.1.13
- Spring 3.1.2.RELEASE
- Maven 3
- Eclipse 4.2
- Tomcat 6 or 7
1. Directory Structure
A standard Maven project for demonstration.
2. Project Dependencies
Declares JSF 2, Spring 3, JSR-330 inject, and Tomcat’s dependencies.
pom.xml
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
3. JSF 2 + Spring Integration
Spring’s bean in Spring Ioc context, and JSF’s managed bean in JSF Ioc context, how to make both working together? The solution is defined Spring’s
SpringBeanFacesELResolver
in faces-config.xml
. Check this official Spring guide.
faces-config.xml
version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
org.springframework.web.jsf.el.SpringBeanFacesELResolver
See following 3 examples to inject Spring’s bean in JSF managed bean.
3.1. XML Schema Example
Many developers still prefer to use XML to manage beans. With
SpringBeanFacesELResolver
, just uses EL ${userBo}
to inject Spring’s bean into JSF’s managed bean.
UserBo.java
package com.mkyong.user.bo; public interface UserBo{ public String getMessage(); }
UserBoImpl.java
package com.mkyong.user.bo.impl; import com.mkyong.user.bo.UserBo; public class UserBoImpl implements UserBo{ public String getMessage() { return "JSF 2 + Spring Integration"; } }
UserBean.java – JSF backing bean
package com.mkyong; import java.io.Serializable; import com.mkyong.user.bo.UserBo; public class UserBean{ //later inject in faces-config.xml UserBo userBo; public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String printMsgFromSpring() { return userBo.getMessage(); } }
applicationContext.xml – Declares userBo bean
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
id="userBo" class="com.mkyong.user.bo.impl.UserBoImpl">
faces-config.xml – Declares managed bean and inject userBo
version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
user
3.2. Spring Annotations – Auto Scan
This example is using Spring annotations. Injects like a normal bean with
@ManagedBean
, @Autowired
and @Component
, it just works as expected.
UserBoImpl.java
package com.mkyong.user.bo.impl; import org.springframework.stereotype.Service; import com.mkyong.user.bo.UserBo; @Service public class UserBoImpl implements UserBo{ public String getMessage() { return "JSF 2 + Spring Integration"; } }
UserBean.java
package com.mkyong; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.mkyong.user.bo.UserBo; @Component @ManagedBean @SessionScoped public class UserBean{ @Autowired UserBo userBo; public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String printMsgFromSpring() { return userBo.getMessage(); } }
applicationContext.xml – Enable the component auto scan
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
base-package="com.mkyong" />
Mixed use of both JSF and Spring annotations are working fine, but it look weird and duplicated –
@Component
and@ManagedBean
together. Actually, you can just uses a single @Component
, see following new version, it’s pure Spring, and it works!
UserBean.java
package com.mkyong; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.mkyong.user.bo.UserBo; @Component @Scope("session") public class UserBean{ @Autowired UserBo userBo; public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String printMsgFromSpring() { return userBo.getMessage(); } }
3.3. JSR-330 Annotation
Since Spring 3.0, Spring offer supports for JSR-330 injection standard. Now, you can uses
@Inject
to replace for@Autowired
and @Named
for @Component
. This is recommended to solution, follow JSR-330 standard make the application more portable to other environments, and it works fine in Spring framework.
UserBoImpl.java
package com.mkyong.user.bo.impl; import javax.inject.Named; import com.mkyong.user.bo.UserBo; @Named public class UserBoImpl implements UserBo{ public String getMessage() { return "JSF 2 + Spring Integration"; } }
UserBean.java
package com.mkyong; import javax.inject.Inject; import javax.inject.Named; import org.springframework.context.annotation.Scope; import com.mkyong.user.bo.UserBo; @Named @Scope("session") //need this, JSR-330 in Spring context is singleton by default public class UserBean { @Inject UserBo userBo; public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String printMsgFromSpring() { return userBo.getMessage(); } }
applicationContext.xml – Need component auto scan also
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
base-package="com.mkyong" />
4. Demo
Example in 3.1, 3.2 and 3.3 are doing exactly the thing – Inject
userBo
into JSF bean, just different implementation. Now, create a simple JSF page to show the the result.
default.xhtml
version="1.0" encoding="UTF-8"?>
web.xml
version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
JavaServerFaces
Done, see output : http://localhost:8080/JavaServerFaces/default.jsf
Download Source Code
Download It – JSF2-Spring-Example.zip (31KB)
No comments:
Post a Comment