SeamFramework.orgCommunity Documentation

Chapter 3. Seam Spring - Architecture and Usage

3.1. Accessing Spring artifacts from CDI
3.1.1. Accessing Spring application contexts
3.1.2. Exposing Spring beans as CDI beans
3.2. Importing CDI beans into Spring applications
3.2.1. Registering a BeanManager
3.2.2. Importing a CDI bean as a Spring bean

The functionality of the Seam Spring module is provided by two sets of components:

The Seam Spring module uses the resource producer pattern for accessing Spring contexts and beans from within CDI. The Spring extension is responsible for producing the actual instances. This mechanism allows the Spring beans to participate in regular CDI injection and the injection targets to be agnostic of the provenience of the injected references, enforcing true separation of concerns. Through this mechanisms Spring contexts can be injected as Spring beans too, if necessary.

The resource producer pattern is used for:

The registration of Spring application contexts as CDI beans is a prerequisite for accessing the Spring beans that are created by them.

The Seam Spring module can access two types of contexts:

CDI applications can install Spring contexts as CDI beans by defining producer fields with the following general pattern:

@Produces

@SpringContext
@<Context-Type>
ApplicationContext context;

This will create a CDI bean of the ApplicationContext type. The nature of the context (bootstrapped by the extension, or looked up elsewhere) is controlled by a specific annotation. The supported annotations are detailed in the following subsections.

The Seam Spring module also supports the registration of CDI beans as Spring beans as well. Once CDI beans are imported into a Spring ApplicationContext, they can be injected as regular Spring beans, either via XML or by annotations.

This can be done by using the dedicated CDI namespace, which can be defined as in the following example:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cdi="http://www.jboss.org/schema/seam/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.jboss.org/schema/seam/spring http://www.jboss.org/schema/seam/spring/seam-spring.xsd">

   <!-- bean definitions -->

</beans>