MainOverviewWikiIssuesForumBuildFisheye

Chapter 24. Introduction

24.1. Overview

Compass::Spring aim is to provide closer integration with the springframework. The following list summarizes the main integration points with Spring.

  • Support for a Compass level factory bean, with Spring IOC modelled configuration options.

  • Compass DAO level support (similar to the ORM dao support), with transaction integration and Compass DAO support class.

  • An extension on top of Spring's Hibernate 3 dao support which extends Compass::Gps Hibernate 3 device. Handles Spring proxing of the Hibernate SessionFactory.

  • An extension on top of Spring's OJB dao support which extends Compass::Gps OJB device. Mainly provides non programmatic configuration with OJB.

  • Extension to Spring MVC, providing Search controller (based on Compass::Core search capabilities) and an Index controller (based on Compass::Gps index operation).

24.2. Compass Definition in Application Context

Compass::Spring provides the ability to expose Compass as a Spring bean from an application context file. Application objects that need to access Compass will obtain a reference to a pre-defined instance via bean references. The following is an example of a Spring XML application context definition configuring Compass:

<beans>

    ...

<bean id="compass"
   class="org.compass.spring.LocalCompassBean">

   <property name="resourceLocations">
     <list>
       <value>classpath:org/compass/spring/test/A.cpm.xml</value>
     </list>
   </property>
   <property name="compassSettings">
      <props>
         <prop key="compass.engine.connection">
            target/testindex
         </prop>
         <!-- This is the default transaction handling
              (just explicitly setting it) -->
         <prop key="compass.transaction.factory">
            org.compass.core.transaction.LocalTransactionFactory
         </prop>
      </props>
   </property>
</bean>

    ...

</beans>

If using a Spring PlatformTransactionManager, you should also initialize the transactionManager property of the LocalCompassBean.

Also, of storing the index within a database, be sure to set the dataSource property of the LocalCompassBean. It will be automatically wrapped by Spring's TransactionAwareDataSourceProxy if not wrapped already.

When using Compass code within an already managed code (within a transaction), it is enough to just use Compass#openSession(), without worrying about Compass transaction management code, or even closing the session. Since even opening the session should not be really required, a LocalCompassSessionBean can be used to directly inject CompassSession to be used. It can be initialized with a Compass instance, but if there is only one within Spring application context, it will automatically identify and use it (this feature is similar to @CompassContext annotation explained later).

Compass also supports @CompassContext annotations to inject either Compass instance or CompassSession instance. The annotation can be used on either a class field or on a property setter. In order to inject the annotation, the bean org.compass.spring.support.CompassContextBeanPostProcessor need to be added to the bean configuration. If Spring 2 new schema based support is used, compass:context can be used.

Compass Spring integration also supports Spring 2 new schema based configuration. Using Compass own schema definition, the configuration of a Compass instance can be embedded within a Spring beans schema based configuration. Here is an example of using the new schema based configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:compass="http://www.compass-project.org/schema/spring-core-config"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.compass-project.org/schema/spring-core-config http://www.compass-project.org/schema/spring-compass-core-config-2.2.xsd">

    <bean id="transactionManager" ...>
      ...
    </bean>

    <compass:compass name="compass" txManager="transactionManager">
        <compass:connection>
            <compass:file path="target/test-index" />
        </compass:connection>
    </compass:compass>

    <!-- A direct LocalCompassSessionBean, used with code within a transaciton context -->
    <compass:session id="sess" />
</beans>