Pages

Saturday, September 27, 2014

Spring Data Neo4j Namespace Configuration Fails on Reload

I started a graph DB project using Neo4j a couple of weeks ago and decided to use Spring Data Neo4j to make my life easier. I got my app up without much hassle using the Spring Data Neo4j namespace configuration to configure my application in XML. This is quickly done with just 2 lines of XML:
<neo4j:config storeDirectory="/var/neo4j" base-package="graph.model" />
<neo4j:repositories base-package="graph.repositories"/>
Everything worked fine until I embedded my project in a larger Spring project which loads around 15 or so top level application context files for all the modules. Suddenly my configuration started throwing exceptions:

java.lang.ExceptionInInitializerError
[...]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.data.neo4j.config.Neo4jConfiguration#0': Unsatisfied dependency expressed through bean property 'conversionService': : Error creating bean with name 'org.springframework.data.neo4j.config.Neo4jConfiguration#2': Unsatisfied dependency expressed through bean property 'conversionService': : Error creating bean with name 'neo4jConversionService': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'neo4jConversionService': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.data.neo4j.config.Neo4jConfiguration#2': Unsatisfied dependency expressed through bean property 'conversionService': : Error creating bean with name 'neo4jConversionService': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'neo4jConversionService': Requested bean is currently in creation: Is there an unresolvable circular reference?

Following the log it turns out that my configuration file was loaded several times. That's nothing new I've seen that a lot in larger Spring projects. But I never dug deep into how Spring does it's dependency resolution and I couldn't figure out what caused this configuration file, which was brand new and clearly not actively loaded by some other module, to be reloaded. However it seems that the namespace configuration of Spring Data Neo4j is not immune against that causing the namespace handler to create circular references. My guess is that something goes wrong when the Neo4jConfigation bean get the conversion service injected - if I remember correctly that is a non-required auto-wire.

It turned out that at least in our case the solution was rather simple. Adding a component scan to my configuration made the problem go away with the file still being loaded multiple times. So just adding
<context:component-scan base-package="org.springframework.data.neoj"/>
on top of  my config made it all work.


No comments:

Post a Comment