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.

About these ads

10 Responses to Annotate JSF Beans with Spring 2.5

  1. Rene says:

    Any idea whether there is a difference between:
    # @Component
    # @Scope(“request”)
    # @Qualifier(“saveDVDBean”)

    and:

    @Controller(“clientSI”)
    @Scope(“request”)
    public class ClientSI extends BaseSI implements BaseSIIntf {

    @Resource(name = “clientManager”)
    public void setManager(ClientManager manager) {
    this.manager = manager;
    }

    At least my managed bean seem to work but I am very confused with all those annotations

  2. cagataycivici says:

    Yes, @Controller will work too but afaik it’s for Spring MVC framework and methods of a class annotated with @Controller are scanned for request mappings. You dont need that in JSF. @Component is simpler and enough.

  3. Sakuraba says:

    wow… Spring is just like Seam. Never thought this would happen after all the JBoss vs Spring wars.

  4. Alexander Lendl says:

    great!

    … but how do I replace the stuff from the faces-config with spring annotations?

    e.g.

    saveDVDBean

    request

    id
    #{param.id}

    thanx!

  5. Alexander Lendl says:

    sorry… the manged bean stuff is not displayed as desired… next try:

    saveDVDBean
    ...
    request

    id
    #{param.id}

  6. Pingback: Annotate JSF Beans with Spring 2.5 « Cagatay Civici’s Weblog

  7. Arisson Leal says:

    Hi,

    When i need to send a get parameter in jsf we define this way in the faces-config.xml

    id
    #{param.id}

    Using the spring to managed my beans how can i do this ?

  8. Arisson Leal says:

    Hi,

    When i need to send a get parameter in jsf we define this way in the faces-config.xml

    id
    #{param.id}

    Using the spring to managed my beans how can i do this ?

  9. Tomer says:

    Hi Man,

    great post, the annotations works great. But i have a problem.
    I am getting the service in the jsf back bean and everything is autowiring just great, the only
    problem is that when i am trying to use findAll, i am getting an empty list, although i have
    rows in my db.
    The only difference from your example is that i am using sessionFactory & not entityManager.

    if you have any ideas….

  10. Tyler says:

    Cool article! I’m sure you’ve been asked this a million times, but would you be able to post/email an url to a working example (zip or jar perhaps?). Really looking forward to trying this out. I have just finished a project with JSF and Hibernate, and think your approach would fit great with my next projects!

Follow

Get every new post delivered to your Inbox.

Join 110 other followers

%d bloggers like this: