Chapter 25. Introduction

25.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).

25.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:



<bean id="compass"

   <property name="resourceLocations">
   <property name="compassSettings">
         <prop key="compass.engine.connection">
         <!-- This is the default transaction handling
              (just explicitly setting it) -->
         <prop key="compass.transaction.factory">



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 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=""

    <bean id="transactionManager" ...>

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

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