Spring Components

Fabric3 can host Spring applications, expose bean as remote endpoints, and wire beans remotely.

Using Spring

Spring application contexts are configured as SCA components using the implementation.spring element in a composite:

<composite ....>

   <component name="MyComponent">
       <implementation.spring location="META-INF/appContext.xml"/>
   </component>

</composite>

The location attribute of the implementation.spring element refers to the application context file that declares the Spring bean.

To expose a bean as an SCA endpoint, and SCA service must be declared in the application context and the service must then be configured on the implementation.spring entry. In the Spring application context, the SCA service should be declared using the SCA namespace:

<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"
   xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans[http://www.springframework.org/schema/beans/spring-beans-3.0.xsd]
      [http://docs.oasis-open.org/ns/opencsa/sca/200912] [http://docs.oasis-open.org/opencsa/sca-assembly/sca-1.1.xsd]">

   <sca:service name="TheSCAService" target="TheSpringBean"/>

   <bean id="TheSpringBean" class="..."/>

</beans>

The service is then configured with a binding as follows:

<composite ....>

   <component name="MyComponent">
       <implementation.spring location="META-INF/appContext.xml"/>
       <service name="TheSCAService">
          <f3:binding.rs uri="service"/>
       </service>
   </component>

</composite>

To use an SCA reference in your Spring bean, use the sca:reference element in the application context:

<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"
   xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans[http://www.springframework.org/schema/beans/spring-beans-3.0.xsd]
      [http://docs.oasis-open.org/ns/opencsa/sca/200912] [http://docs.oasis-open.org/opencsa/sca-assembly/sca-1.1.xsd]">

   <bean id="TheSpringBean" class="... .MyClass">
      <property name="myRefBean" ref="theRefService"/>
   </bean>

   <sca:reference name="theRefService" type="foo.bar.MyServiceI"/>

</beans>

The reference can then be bound or wired in the composite containing the application context:

<composite ....>

   <component name="MyComponent">
       <implementation.spring location="META-INF/appContext.xml"/>
       <reference name="theRefService" target="TheService"/>
   </component>

</composite>

SCA schema file referenced in Spring configuration (http://docs.oasis-open.org/opencsa/sca-assembly/sca-1.1.xsd) does not actually exist at all. This may give XML validation errors in your IDE and it also makes Fabric3 to start really slow with seemingly random warnings or even errors from Xerces XML validator validating Spring configuration files.

Currently this can be bypassed by disabling XML validation in IDE and adding the following configuration element into conf/systemConfig.xml:

<spring validating="false"/>

JPA and Hibernate in Spring

Fabric3 integrates its it transaction and JPA/Hibernate infrastructure with Spring. Fabric3 provides a Spring Bean org.fabric3.implementation.spring.api.Fabric3EntityManagerFactoryBean, that extends the standard Spring LocalContainerEntityManagerFactoryBean. This gives the ability to have pooled datasources and it makes it unnecessary to specify a persistence provider in persistence.xml. With this approach, vendor-specific configurations can be avoided in application artifacts.

<beans>
   <bean id="EntityManagerFactory" class="org.fabric3.implementation.spring.api.Fabric3EntityManagerFactoryBean"> 
      <property name="persistenceUnitName" value="unitName"/> 
   </bean>

   <bean id="PersistenceAnnotationBeanPostProcessor" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>  

   <tx:annotation-driven/>
<beans>

Alternatively, the standard Spring LocalContainerEntityManagerFactoryBean can be used instead of the Fabric3EntityManager. In that case, the naming of the datasource needed, should be placed in the persistence.xml:

<beans>
   <bean d="EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
      <property name="persistenceUnitName" value="unitName"/>
      <property name="dataSource" ref="dataSourceName"/> 
   </bean>
<beans>

JTA in Spring

Fabric3 supports Spring JDK Proxy-based AOP and transactions. (Use the Spring @Transactional annotation to use transactional behavior).

Fabric3 JTA transaction manager and pooled datasources are automatically aliased in each Spring application context. This provides full transactional support (including XA and pooled datasources) to Spring persistence (JPA, Hibernate, JDBC) and makes it also possible to automatically propagate transactions from other SCA components and bindings to Spring Beans.

Enabling Spring

There are two extensions to enable Spring support in Fabric3: the Spring implementation type extension and an extension that supplies the Spring library classes. By separating these two extensions, it is possible to use custom versions of the Spring library extension. Under the covers, the Spring implementation extension will use the Fabric3 extension point mechanism to resolve the Spring library extension. To create a custom library extension, a jar containing other jars in META-INF/lib/ and an SCA manifest needs to be produced.

To install Spring support into the Fabric3 runtime, download the Spring profile from the Fabric3 web site and copy the appropriate jars into the correct directory. To find out in which directory these jars should be copied, read the runtime layout description. After restarting the runtime, Spring support will be activated.