Chapter 20. Spring Framework integration
The Spring integration module allows easy migration of Spring-based projects to Seam and allows Spring applications to take advantage of key Seam features like conversations and Seam's more sophisticated persistence context management.
20.1. Injecting Seam components into Spring beans
Injecting Seam component instances into Spring beans is accomplished using the <seam:instance/>
namespace handler. To enable the Seam namespace handler, the Seam namespace must be added to the Spring beans definition file:
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:seam="https://jboss.com/products/seam/spring-seam"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-2.0.xsd
https://jboss.com/products/seam/spring-seam
https://jboss.com/products/seam/spring-seam-1.2.xsd">
Now any Seam component may be injected into any Spring bean:
<bean id="someSpringBean" class="SomeSpringBeanClass" scope="prototype">
<property name="someProperty">
<seam:instance name="someComponent"/>
</property>
</bean>
An EL expression may be used instead of a component name:
<bean id="someSpringBean" class="SomeSpringBeanClass" scope="prototype">
<property name="someProperty">
<seam:instance name="#{someExpression}"/>
</property>
</bean>
Seam component instances may even be made available for injection into Spring beans by a Spring bean id.
<seam:instance name="someComponent" id="someSeamComponentInstance"/>
<bean id="someSpringBean" class="SomeSpringBeanClass" scope="prototype">
<property name="someProperty" ref="someSeamComponentInstance">
</bean>
Now for the caveat!
Seam was designed from the ground up to support a stateful component model with multiple contexts. Spring was not. Unlike Seam bijection, Spring injection does not occur at method invocation time. Instead, injection happens only when the Spring bean is instantiated. So the instance available when the bean is instantiated will be the same instance that the bean uses for the entire life of the bean. For example, if a Seam CONVERSATION
-scoped component instance is directly injected into a singleton Spring bean, that singleton will hold a reference to the same instance long after the conversation is over! We call this problem
scope impedance
. Seam bijection ensures that scope impedance is maintained naturally as an invocation flows through the system. In Spring, we need to 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>
This example shows one way to use a Seam-managed persistence context from a Spring bean. (A more robust way to use Seam-managed persistence contexts as a replacement for the Spring OpenEntityManagerInView
filter will be provided in a future release)