Chapter 26. Spring Framework integration

The Spring Framework is part of the Seam inversion-of-control (IoC) module. It allows easy migration of Spring-based projects to Seam, and benefits Spring applications with Seam features, such as conversations and a more sophisticated persistence context management.

Note

The Spring integration code is included in the jboss-seam-ioc library. This library is a required dependency for all Seam-Spring integration techniques covered in this chapter.
Seam's support for Spring gives you:
  • Seam component injection into Spring beans,
  • Spring bean injection into Seam components,
  • Spring bean to Seam component transformation,
  • the ability to place Spring beans in any Seam context,
  • the ability to start a spring WebApplicationContext with a Seam component,
  • support for using Spring PlatformTransactionManagement with your Seam-based applications,
  • support for using a Seam-managed replacement for Spring's OpenEntityManagerInViewFilter and OpenSessionInViewFilter, and
  • support for backing @Asynchronous calls with Spring TaskExecutors.

26.1. Injecting Seam components into Spring beans

Inject Seam component instances into Spring beans with the <seam:instance/> namespace handler. To enable the Seam namespace handler, the Seam namespace must first be added to the Spring beans definition file:
<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">
Any Seam component can now be injected into any Spring bean, like so:
<bean id="someSpringBean" class="SomeSpringBeanClass" scope="prototype">
  <property name="someProperty">
    <seam:instance name="someComponent"/>
  </property>
</bean>
You can use an EL expression instead of a component name:
<bean id="someSpringBean" class="SomeSpringBeanClass" scope="prototype">
  <property name="someProperty">
    <seam:instance name="#{someExpression}"/>
  </property>
</bean>
You can inject a Seam component instance into a Spring bean by using a Spring bean ID, like so:
<seam:instance name="someComponent" id="someSeamComponentInstance"/>

<bean id="someSpringBean" class="SomeSpringBeanClass" scope="prototype">
  <property name="someProperty" ref="someSeamComponentInstance">
</bean>

However, Spring, unlike Seam, was not designed to support a stateful component model with multiple contexts. Spring injection does not occur at method invocation time, but when the Spring bean is instantiated.
The instance available when the bean is instantiated will be used for the entire life of the bean. Say you inject a Seam conversation-scoped component instance directly into a singleton Spring bean — that singleton will hold a reference to the same instance long after the conversation is over. This is called scope impedance.
Seam bijection maintains scope impedance naturally as an invocation flows through the system. In Spring, we must inject a proxy of the Seam component, and resolve the reference when the proxy is invoked.
The <seam:instance/> tag lets us automatically proxy the Seam component.
<seam:instance id="seamManagedEM" 
               name="someManagedEMComponent" 
               proxy="true"/>

<bean id="someSpringBean" class="SomeSpringBeanClass">
  <property name="entityManager" ref="seamManagedEM">
</bean>
Here, we see one example of using a Seam-managed persistence context from a Spring bean. See the section on Section 26.6, “Using a Seam-Managed Persistence Context in Spring” for a more robust way to use Seam-managed persistence contexts as a replacement for the Spring OpenEntityManagerInView filter.