Spring Modules JCR Node Creation & Retrieval

David Winterfeldt

2008


This example shows how to create and retrieve nodes and files (content nodes) using Spring Modules JCR (Java Content Repository (JSR-170)) module. 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.

JcrNodeCreationIT-context.xml
                
<?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:p="http://www.springframework.org/schema/p"
       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 />
    
    <!-- normal repository -->
    <bean id="repository"
          class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean"
          p:configuration="classpath:/jackrabbit-repository.xml"
          p:homeDir="file:./target/repo" />

    <bean id="sessionFactory"
          class="org.springmodules.jcr.jackrabbit.JackrabbitSessionFactory"
          p:repository-ref="repository" />

    <bean id="jcrTemplate" 
          class="org.springmodules.jcr.JcrTemplate"
          p:sessionFactory-ref="sessionFactory"
          p:allowCreate="true" />
    
</beans>