John
John

Reputation: 11831

Spring Data Neo4j and Grails 3

Does anyone have Spring Data Neo4j (3.3.0) working with Grails v3 and is willing to share the configuration?

I'd like to get the web interface working (as per here: How to enable neo4j webadmin when using spring-data-neo4j?), but I'm getting a bit lost when trying to work out what should go in Application.groovy, as Grails extends it from GrailsAutoConfiguration and the example shown extends it from Neo4jConfiguration.

Upvotes: 4

Views: 325

Answers (1)

John
John

Reputation: 11831

Add the following dependencies to build.gradle:

compile("org.springframework.data:spring-data-neo4j")
compile "org.neo4j.app:neo4j-server:2.1.5"
compile "org.neo4j.app:neo4j-server:2.1.5:static-web"

The resources files below go in conf/spring.

resources.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:spring-configured/>
    <context:annotation-config/>

    <util:map id="config">
        <entry key="remote_shell_enabled" value="true"/>
    </util:map>

    <neo4j:config storeDirectory="target/data/db" base-package="com.example"/>

    <neo4j:repositories base-package="com.example.repositories"/>

    <bean id="graphDbFactory" class="org.neo4j.graphdb.factory.GraphDatabaseFactory"/>

    <bean id="graphDbBuilder" factory-bean="graphDbFactory" factory-method="newEmbeddedDatabaseBuilder">
        <constructor-arg value="target/data/db"/>
    </bean>

    <bean id="graphDbBuilderFinal" factory-bean="graphDbBuilder" factory-method="setConfig">
        <constructor-arg ref="config"/>
    </bean>

    <bean id="graphDatabaseService" factory-bean="graphDbBuilderFinal" factory-method="newGraphDatabase"
    destroy-method="shutdown"/>

    <bean id="serverWrapper" class="org.neo4j.server.WrappingNeoServerBootstrapper" init-method="start"
        destroy-method="stop">
        <constructor-arg ref="graphDatabaseService"/>
    </bean>
</beans>

resources.groovy (I'm sure this could have a lot added to it from resources.xml, but I'm not sure what to do for this):

import com.example.MyGraph;

// Place your Spring DSL code here
beans = {
    myGraph(MyGraph)

}

src/main/groovy/com/example/MyGraph.java:

package com.example;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
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.Neo4jConfiguration;
import org.springframework.data.neo4j.core.GraphDatabase;

@Configuration
public class MyGraph extends Neo4jConfiguration {

    @Autowired
    GraphDatabase graphDatabase;

}

I put my domain classes for Neo4J in src/main/groovy/com/example/domain and the repository classes in src/main/groovy/com/example/repositories. The Spring example code then works fine. There is a web server admin interface for Neo4J available on port 7474 when you run grails run-app.

Upvotes: 1

Related Questions