Spring by Example JCR Module

David Winterfeldt

2008


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"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 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"
        class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean">
        <!-- 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>

    <bean id="sessionFactory"
          class="org.springmodules.jcr.jackrabbit.JackrabbitSessionFactory">
        <property name="repository" ref="repository" />
    </bean>

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

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