Sheila's Blog

Software Development

Sheila's Blog header image 2

Configuring seam to integrate spring

January 5th, 2010 by Sheila

There are advantages and disadvantages to choosing Spring or Seam for developing an application. The third option is to use both of them and leverage the best of both frameworks. If Spring and Seam are integrated then they can pass components to each other to use depending on which framework can construct that component in the best possible way. The instructions below are based on Seam 2.2.0.GA and Spring 3.0.0.

In order to integrate Spring into Seam, you need the ability to start up both of the containers and allow them access to each others components. To assist with this Seam provides a special ContextLoader in the jboss-seam-ioc jar file, so that Seam can read the Spring configuration files and start up the Spring container. To configure this you need to edit Seam’s components.xml and add the following namespace and schema entry: <?xml version="1.0" encoding="UTF-8"?> <components ... xmlns:spring="http://jboss.com/products/seam/spring" ... http://jboss.com/products/seam/spring http://jboss.com/products/seam/spring-2.2.xsd

Further down the file within the components section you instruct Seam to bootstrap the Spring container, assuming by default that there is a WEB-INF/applicationContext.xml containing the Spring configuration. <spring:context-loader/>

You then add a basic applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:seam="http://jboss.com/products/seam/spring-seam" 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.0.xsd http://jboss.com/products/seam/spring-seam http://jboss.com/products/seam/spring-seam-2.2.xsd"> </beans>

Any additional spring configuration will be added to this file.

For this code to work, you need to add the following jar files to your ear file. The jboss-seam-ioc.jar contains the classes that allow you to integrate Spring and the other jars are required for the Spring configuration files.

jboss-seam-ioc-2.2.0.GA.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar

You can now add a spring bean into your applicationContext.xml <bean id="exampleSpringBean" class="com.example.exampleSpringBean"> <property name="testString" value="This is a test" /> <seam:component/> </bean>

You will obviously need to create a corresponding class for this bean. The seam:component tag marks this bean as a SpringComponent which is registered for use as a Seam component by the Seam container.

You can now inject the bean into another class as a seam component. @In(create=true) private ExampleSpringBean exampleSpringBean; ... String value = exampleSpringBean.getTestString();

This is a very simple example with basic configuration that doesn’t take into account any of the issues you may need to watch out for when mixing Spring and Seam.

Tags: No Comments

Leave a Comment

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.