Using Spring to Manage JSF Beans
The traditional way to integrate JSF and Spring was to define JSF beans in faces-config as managed beans and refer to the spring beans using the managed-property configuration. With the help of the spring’s delegatingvariableresolver the managed property is resolved from spring application context and JSF’s IOC injects the bean to the JSF Managed bean instance. I’ve written
an article it about this way before.First approach is modelled as follows
And the better approach described in this article
Although the way described above sounds nice at first glance, I believe it has a major issue, do we really need to have two IOC containers in your application to do dependency injection. I mean why not just one right? Actually this was necessary before spring 2 but with the addition of custom scopes, a better way for integration has come up. Now spring has the ability manage the JSF backing beans in it’s container, this results in a faces-config.xml with no managed-bean configurations. I’ve created a simple DVDStore application to show a sample configuration. Let’s start with the domain class DVD
01 | package com.cc.blog.dvdstore.domain |
06 | private Integer discs; |
12 | public DVD(String title, Integer discs) { |
17 | public Integer getDiscs() { |
21 | public void setDiscs(Integer discs) { |
25 | public String getTitle() { |
29 | public void setTitle(String title) { |
DVDService lives at service layer
1 | package com.cc.blog.dvdstore.service; |
3 | import com.cc.blog.dvdstore.domain.DVD; |
5 | public interface IDVDService { |
7 | public void saveDVD(DVD dvd); |
01 | package com.cc.blog.dvdstore.service; |
03 | import com.cc.blog.dvdstore.domain.DVD; |
05 | public class DVDService implements IDVDService{ |
07 | public void saveDVD(DVD dvd) { |
And the JSF Backing to handle the creation of a new DVD record
01 | package com.cc.blog.dvdstore.view; |
03 | import com.cc.blog.dvdstore.domain.DVD; |
04 | import com.cc.blog.dvdstore.service.DVDService; |
06 | public class SaveDVDBean { |
09 | private IDVDService service; |
11 | public SaveDVDBean() { |
22 | public void setDvd(DVD dvd) { |
26 | public IDVDService getService() { |
30 | public void setService(IDVDService service) { |
31 | this .service = service; |
34 | public String save() { |
Fragment from the JSF page backed by SaveDVDBean,
02 | < h:panelGrid columns = "2" > |
03 | < h:outputLabel for = "title" >h:outputLabel > |
04 | < h:inputText id = "title" value = "#{saveDVDBean.dvd.title}" >h:inputText > |
06 | < h:outputLabel for = "discs" >h:outputLabel > |
07 | < h:inputText id = "discs" value = "#{saveDVDBean.dvd.discs}" >h:inputText > |
09 | < h:commandButton value = "Save" action = "#{saveDVDBean.save}" >h:commandButton > |
web.xml
01 | xml version = "1.0" encoding = "UTF-8" ?>
|
06 | < param-name >contextConfigLocationparam-name > |
07 | < param-value >/WEB-INF/applicationContext.xmlparam-value > |
11 | < listener-class >org.springframework.web.context.ContextLoaderListenerlistener-class > |
14 | < listener-class >org.springframework.web.context.request.RequestContextListener< listener-class > |
18 | < servlet-name >Faces Servletservlet-name > |
19 | < servlet-class >javax.faces.webapp.FacesServletservlet-class > |
20 | < load-on-startup >1< load-on-startup > |
23 | < servlet-name >Faces Servletservlet-name > |
24 | < url-pattern >*.jsfurl-pattern > |
applicationContext.xml
01 | xml version = "1.0" encoding = "UTF-8" ?>
|
04 | default-autowire = "byName" |
10 | < bean id = "dvdService" class = "com.cc.blog.dvdstore.service.DVDService" >bean > |
12 | < bean id = "saveDVDBean" class = "com.cc.blog.dvdstore.view.SaveDVDBean" scope = "request" > |
13 | < property name = "service" > |
14 | < ref bean = "dvdService" /> |
faces-config.xml
01 | xml version = "1.0" encoding = "UTF-8" ?>
|
07 | org.springframework.web.jsf.DelegatingVariableResolver |
As seen above, faces-config has no managed-bean stuff, instead saveDVDBean is defined in spring’s application context in request scope. So here is why using Spring’s IOC to manage JSF backing beans is cool:)
- With spring aop, you can apply aop tricks to your jsf beans, for example you can do aop audit logging based on jsf events like SaveDVDBean’s save method. Add an annotation like @Auditable to the save() method, write an interceptor and then you can log who and when tried to save a dvd.
- Use autowiring on JSF beans
- Constructor Injection
- Lifecycle events like afterPropertiesSet using lifecycle interfaces or configurations like init-method, destroy-method
- Much more actually, see spring ioc container documents for more.
1 comment:
Great Article
JSF Online Training | JSF Training | JSF Training Courses | Java Training Institutes in Chennai | Java Training in Chennai | Java Course in Chennai | JSF Interview Questions
Post a Comment