JSFDays08 Vieanna

JSFDays08 will be hold in Vienna, Austria on 12-14 of March. Whole JSF community will be there and it’s a nice opportunity if you are following JSF. I’ll be there as a speaker, my session is on the last day but I’m planning to arrive early and see some of the famous places in Vienna.

Speakers list(Not final);

http://conference.irian.at/conference/main/speakers.jsf

See you in Vienna!

Annotate JSF Beans with Spring 2.5

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.

End of Military Duty, I’m Back in Action

From the beginning of 2007 until end of November, I served my military duty that is mandatory in Turkey. Although I was classified as a military developer and continue doing my job during this period, I was a bit away from the open source world because of lack of time, now as a free man and as a freelancer I’m fully back in action, blogging, coding, developing, oh yeah! Also I’m co-authoring APRESS’s “The Definitive Guide to Apache MyFaces and Facelets” book mainly with the Apache Trinidad section, keep an eye on this blog for more news.