After the arrival of cool annotation driven configuration features of in spring 2.5, I’ve decided to update my DVDStore example that
is using Spring to manage JSF beans to a annotation based one. If you wonder the Spring 2.0 version my old article is here.
Update process is quite easy, three parts are changed;
- applicationContext.xml
- DVDService.java
- SaveDVDBean.java
About spring xml config, just removed the metadata related to the jsf bean(SaveDVDBean) and the spring service(DVDService). The new applicationContext.xml looks like this;
<beans 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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.cc.blog.dvdstore" />
</beans>
- Note that you don’t need <context:annotation-config />, that’s because <context:component-scan takes care of including AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor.IDVDService implementation needs an @Service annotation.
package com.cc.blog.dvdstore.service;
import org.springframework.stereotype.Service;
import com.cc.blog.dvdstore.domain.DVD;
@Service
public class DVDService implements IDVDService{
public void saveDVD(DVD dvd) {
//Refer to a DAO-Repository implementation and persist the DVD
}
}
Last change is on the backing bean;
package com.cc.blog.dvdstore.view;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.cc.blog.dvdstore.domain.DVD;
import com.cc.blog.dvdstore.service.IDVDService;
@Component
@Scope("request")
@Qualifier("saveDVDBean")
public class SaveDVDBean {
private DVD dvd;
@Autowired
private IDVDService dvdService;
public SaveDVDBean() {
//NOop
}
public DVD getDvd() {
if(dvd == null)
dvd = new DVD();
return dvd;
}
public void setDvd(DVD dvd) {
this.dvd = dvd;
}
public String save() {
dvdService.saveDVD(dvd);
return null;
}
}
- Note that getter&setter for dvdservice is not required since dependendency is injected with @AutoWired
- The jsf backing bean is annotated with @Component marking this class as a component.
- Scope is set to request scope using the @Scope(“request”)
- IDVDService dependency is injected through @AutoWired annotation.
- @Qualifier is important since it specifies the name of the jsf bean, this name is referenced in jsf pages.
Rest of the related classes and config files do not change, faces-config.xml, web.xml, IDVDService and etc, they stay just same as in my previous spring 2.0 example.
That’s all, now the jsf-spring integration is done with annotations, SaveDVDBean can be accessed in jsf views via #{saveDVDBean.bla.bla}. This is a great way if you’re bored of writing <managed-bean> stuff in faces-config.xml or <bean> stuff in spring. Plus you get all the cool annotation driven spring features like @Resource, @PreConstruct, @PostConstruct, @AutoWire and etc. and apply them to jsf backing beans.