Jackrabbit with Spring
I was searching for a way to use Jackrabbit with Spring. There are articles about this topic, but each of them suggests using spring-modules. Now, my problem with spring-modules jcr implementation is, that they depend on non-released or obsolete 3rd party JARs, like apravez.* and jeceira. Just take a look at the pom file, and you’ll see.
On the other hand I do not feel a need for jcrTemplate, jcrCallback or any other ORM flavor helpers. I just need a nice, easy integration of Jackrabbit, where I can easily inject JCR Session into any class with spring.
The JNDI way would do, but I do not want to use JNDI, I want to configure Jackrabbit from my spring beans.
Now with those premises, I came out with a quite easy setup, that follows below.
Spring configuration:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean name="jcrConfiguration"
class="org.apache.jackrabbit.core.config.RepositoryConfig"
factory-method="create">
<constructor-arg index="0"
type="java.io.InputStream"
value="classpath:repository.xml" />
<constructor-arg index="1"
type="java.lang.String"
value="/tmp/repository" />
</bean>
<bean name="jcrRepository" factory-method="create"
class="org.apache.jackrabbit.core.RepositoryImpl">
<constructor-arg>
<ref bean="jcrConfiguration"/>
</constructor-arg>
</bean>
<bean name="jcrSession"
factory-bean="jcrRepository"
factory-method="login"
scope="session"
destroy-method="logout">
</bean>
</beans>
The configuration of Jackrabbit is defined in repository.xml file, – I just took the default repository.xml, the one that is created when running TransientReposiotry for the first time. Of course for better integration and configuration define your own. There is one more configuration point and that could be seen in the XML from above. Spring bean jcrConfiguration defines where the repository.xml configuration is and the path to repository resources, – the second argument. For test purpose I just used /tmp/repository but for production you might set it to something like: repository Do not forget to set up spring RequestContextListener or you’ll be missing session (and request) scope in spring configurations. In web.xml just define a listener:
org.springframework.web.context.request.RequestContextListener
From code we’ll just going to use the jcrSession. Here is a small example for usage:
@Service
public class TopicService implements BeanFactoryAware {
private BeanFactory beanFactory;
public Session getJcrSession() {
Session jcrSession =
(Session) beanFactory.getBean("jcrSession");
return jcrSession;
}
@Override
public void setBeanFactory(BeanFactory beanFactory)
throws BeansException
{
this.beanFactory = beanFactory;
}
public void myFucntion() {
Session session = getJcrSession();
// here you do what you would do with JCR...
}
}
As my service is singleton I inject Spring Bean Factory with the help of org.springframework.beans.factory.BeanFactoryAware mixin, so each time I call getJcrSession() I get a JCR session bind to HTTP session.
Well, that’s it. A simple configuration, easy usage.