Spring by Example JCR Module

David Winterfeldt


The Spring by Example JCR module uses Spring Modules JCR (Java Content Repository) module. Currently the utilities provide a way to recurse through the repositories nodes using JcrTemplate and a custom node callback for each matching node found while recursing the repository. This example uses Apache Jackrabbit for the Java Content Repository which is the reference implementation for JSR-170.

1. Spring Configuration

The first bean definition defines defines the Jackrabbit repository by specifying the configuration file to use and the location of the repository. If the repository doesn't already exist, it will be created on startup. The next bean creates a session factory based on the respository and the next one creates a JcrTemplate using the session factory.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <!-- Register Annotation-based Post Processing Beans -->
    <context:annotation-config />

    <!-- Scan context package for any eligible annotation configured beans. -->
    <context:component-scan base-package="org.springbyexample.jcr" />
    <!-- normal repository -->
    <bean id="repository"
        <!-- normal factory beans params -->
        <property name="configuration" value="classpath:/jackrabbit-repository.xml" />
        <!-- use the target folder which will be cleaned  -->
        <property name="homeDir" value="file:./target/repo" />

    <bean id="sessionFactory"
        <property name="repository" ref="repository" />

    <bean id="jcrTemplate" class="org.springmodules.jcr.JcrTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="allowCreate" value="true" />

    <bean id="processor" class="org.springbyexample.jcr.JcrContentExporter">
        <property name="contentRecurser" ref="jcrContentRecurser" />
        <property name="rootFolder" value="./target/repo_export" />