Archive

Archive for the ‘spring’ Category

Jackrabbit with Spring

No Gravatar

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:



  
    
    
  

  
    
      
    
  

  
  

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.

Share