The most common usage of Compass is within a Spring enabled application. If the application uses a Compass within a Spring bean, and Java 5, you can now use @CompassContext in order to inject either a Compass or a CompassSession instance.
The following simple dao uses the @CompassContext annotation:
public class CompassContextDao { @CompassContext Compass compass; CompassSession session; @CompassContext
protected void setCompassSession(CompassSession session) {
this.session = session;
}
}
The dao uses both Compass and CompassSession and shows how the annotations can be used (both on field and setter method). Note, when the dao already uses Spring declarative transactions for its operations (or has a service layer that has it), one can inject the CompassSession and just use it, without worrying about Compass transaction management (see previous blog entry).
Compass uses spring bean post processor and it must be configured within Spring configuration in order to enable this feature. Here is an example of Spring configuration file:
<bean class=“org.compass.spring.support.CompassContextBeanPostProcessor”/><
compass:compass name=“compass”>
<compass:connection>
<compass:file path=“target/test-index” />
</compass:connection>
<compass:mappings>
<compass:class name=“org.compass.spring.test.context.A” />
</compass:mappings>
</compass:compass><
bean id=“dao” class=“org.compass.spring.test.context.CompassContextDao” />
This feature is part of 1.1 M2 SNAPSHOT and beyond. Enjoy!