0
votes

Has anyone managed to get spring-data-neo4j working with Grails in direct data access mode, as documented here?

I can't get Grails to respect the @Autowired tags below. The code below is a very simple version of the example given by spring in their documentation:

package hello;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.core.GraphDatabase;

import java.io.File;

@Configuration
@EnableNeo4jRepositories(basePackages = "hello")
public class Application extends Neo4jConfiguration {

public Application() {
    setBasePackage("hello");
}

@Bean
GraphDatabaseService graphDatabaseService() {
    return new GraphDatabaseFactory().newEmbeddedDatabase("accessingdataneo4j.db");
}

@Autowired
PersonRepository personRepository;

@Autowired
GraphDatabase graphDatabase;

public String test() throws Exception {
    Person greg = new Person("Greg");
    String rc = "";

    Transaction tx = graphDatabaseService().beginTx();
    try {

        personRepository.save(greg);
        rc = personRepository.findByName("Greg").toString();

        tx.success();
    } finally {
        tx.close();
    }

    return rc;
}

}

I get the following error message when performing grails run-app (it's a much longer series of nested exceptions, but this is the main cause):

Error creating bean with name 'graphDatabase' defined in class path resource [hello/TestGraph.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.core.GraphDatabase org.springframework.data.neo4j.config.Neo4jConfiguration.graphDatabase()] threw exception; nested exception is java.lang.IllegalArgumentException: requirement failed: Can't work with a null graph database

I've tried putting the following into Config.groovy:

grails.spring.bean.packages = ['org.neo4j.kernel', 'hello']

This should mean that I don't need to put anything in spring/resources.groovy, no?

In any case, it doesn't seem to work if I do this in it anyway:

beans = {
    graphDatabase(GraphDatabase) {}
}

I get a different exception to start with, before getting the same exceptions as before:

spring.RuntimeSpringConfigUtilities [RuntimeConfiguration] Unable to load beans from resources.groovy groovy.lang.MissingPropertyException: No such property: GraphDatabase for class: resources at resources$_run_closure1.doCall(resources.groovy:4) at grails.spring.BeanBuilder.invokeBeanDefiningClosure(BeanBuilder.java:754) at grails.spring.BeanBuilder.beans(BeanBuilder.java:584) at grails.plugin.hibernate4.HibernatePluginSupport.checkExternalBeans(HibernatePluginSupport.groovy:464) at grails.plugin.hibernate4.HibernatePluginSupport$__clinit__closure1.doCall(HibernatePluginSupport.groovy:122) at grails.spring.BeanBuilder.invokeBeanDefiningClosure(BeanBuilder.java:754) at grails.spring.BeanBuilder.beans(BeanBuilder.java:584) at grails.spring.BeanBuilder.invokeMethod(BeanBuilder.java:527) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

2

2 Answers

0
votes

I haven't. Thought that grails had some sort of plug-in for this last time I checked but I am probably wrong. Either way this other person has done it by the looks of it. use spring-data-neo4j in grails get UnsatisfiedDependencyException

0
votes

I've got a working version, as a result of some fantastic help here: Grails @Autowire in Java class not working

The main issue seemed to be that Grails didn't seem to respect the SDN annotations and it was necessary to put the configuration code into spring/resources.xml.

All code is on Github here.