DATAGRAPH-9 Updated documentation for xml-namespace config

This commit is contained in:
Michael Hunger
2011-02-08 00:19:31 +01:00
parent 938f46217f
commit d91f441ec4
10 changed files with 121 additions and 250 deletions

View File

@@ -84,21 +84,35 @@ Configure the Aspect-J maven plugin build & library dependency. Add the followi
Spring Configuration:
* Configure Spring Data Graph for Neo4j in your application using Spring 3.0's [Java based bean metadata](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#new-java-configuration) For XML based configuration refer to the User Guide.
* Configure Spring Data Graph for Neo4j in your application using the provided xml namespace.
@Configuration
public class MyConfig extends AbstractNeo4jConfiguration {
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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:datagraph="http://www.springframework.org/schema/data/graph"
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/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
">
@Override
public boolean isUsingCrossStorePersistence() {
return false;
}
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test"/>
@Override
public GraphDatabaseService graphDatabaseService() {
return new EmbeddedGraphDatabase("target/neo4j-db");
}
}
</beans>
* You can also use the supplied Java @Configuration in Neo4jConfiguration. [Java based bean metadata](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#new-java-configuration)
<context:annotation-config/>
<bean class="org.springframework.data.graph.neo4j.config.Neo4jConfiguration"/>
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase" destroy-method="shutdown">
<constructor-arg index="0" value="target/config-test"/>
</bean>
* Annotate your entity class. In this case it is a 'World' class that has a relationship to other worlds that are reachable by rocket travel:

View File

@@ -1,2 +0,0 @@
Spring Data Graph Project

View File

@@ -1,182 +0,0 @@
Getting started with data-graph-neo4j
=====================================
It provides an annotation-driven object-graph mapping library.
Data-graph-neo4j is to Neo4j what Hibernate is to an RDBMS. It requires
AspectJ and Spring Framework.
More examples using data-graph-neo4j can be found in this repository:
http://github.com/SpringSource/spring-data-graph-examples
Standalone setup of project using data-graph-neo4j
==================================================
1. Maven Dependency
-------------------
Include data-graph-neo4j in your pom.xml:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
2. Aspect-J plugin build & library dependency
---------------------------------------------
Add the following plugin XML to your project's <plugins> config in pom.xml
to hook AspectJ into the build process:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
3. Spring configuration
-----------------------
3.1. Spring XML Config
----------------------
This is a the full Spring XML context configuration to get started. It creates
all dependencies required by the library.
A simpler configuration approach is provided below the full config:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Configure property placeholders -->
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<!-- Enable AspectJ @Configurable support -->
<context:spring-configured/>
<!-- Enable @Repository, @Service, @Component, @Autowired, etc. -->
<context:component-scan base-package="com.springone.petclinic">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.data.graph.neo4j.spi.node.Neo4jConstructorGraphEntityInstantiator" id="graphEntityInstantiator"/>
<bean class="org.springframework.data.graph.neo4j.spi.relationship.ConstructorBypassingGraphRelationshipInstantiator" id="relationshipEntityInstantiator"/>
<bean class="org.springframework.data.graph.neo4j.spi.node.Neo4jNodeBacking" factory-method="aspectOf" id="neo4jNodeBacking"/>
<bean class="org.springframework.data.graph.neo4j.spi.relationship.Neo4jRelationshipBacking" factory-method="aspectOf" id="neo4jRelationshipBacking"/>
<bean class="org.neo4j.kernel.EmbeddedGraphDatabase" destroy-method="shutdown" id="graphDbService" scope="singleton">
<constructor-arg index="0" value="${neo4j.location}"/>
</bean>
<bean class="org.springframework.data.graph.neo4j.support.GraphDatabaseContext" id="graphDatabaseContext"/>
<bean class="org.springframework.data.graph.neo4j.support.SubReferenceNodeTypeStrategy" id="nodeTypeStrategy">
<constructor-arg index="0" ref="graphDatabaseContext"/>
</bean>
<bean class="org.springframework.data.graph.neo4j.fieldaccess.NodeEntityStateAccessorsFactory" id="nodeEntityStateAccessorsFactory"/>
<bean class="org.springframework.data.graph.neo4j.fieldaccess.RelationshipEntityStateAccessorsFactory" id="relationshipEntityStateAccessorsFactory"/>
<bean class="org.springframework.data.graph.neo4j.fieldaccess.NodeDelegatingFieldAccessorFactory" id="nodeDelegatingFieldAccessorFactory">
<constructor-arg index="0" ref="graphDatabaseContext"/>
</bean>
<bean class="org.springframework.data.graph.neo4j.finder.FinderFactory" id="finderFactory">
<constructor-arg index="0" ref="graphDatabaseContext"/>
</bean>
<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
<property name="transactionManager">
<bean class="org.neo4j.kernel.impl.transaction.SpringTransactionManager">
<constructor-arg index="0" ref="graphDbService"/>
</bean>
</property>
<property name="userTransaction">
<bean class="org.neo4j.kernel.impl.transaction.UserTransactionImpl">
<constructor-arg index="0" ref="graphDbService"/>
</bean>
</property>
</bean>
<bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionService"/>
</beans>
3.2 Spring-Java-Config
----------------------
You can also derive from a provided abstract spring configuration class (spring-javaconfig) that already encapsulates all this
configuration and just provide a directory for the graph database:
public class MyConfig extends AbstractNeo4jConfiguration {
@Override
public boolean isUsingCrossStorePersistence() {
return false;
}
@Bean(destroyMethod = "shutDown")
public GraphDatabaseService graphDatabaseService() {
return new EmbeddedGraphDatabase("target/neo4j-db");
}
}
Then your spring xml configuration file gets much simpler:
<beans>
...
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="com.example.config.MyConfig"/>
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
...
</beans>
4. Setup done
-------------
After this is set up, you can just use the annotated POJOs and they will automatically be backed by Neo4j.
The one thing that the user has to do is wrap any data-graph-neo4j usage in Neo4j transactions. (or just use @Transactional)
You should now be set up with the AspectJ configuration in your pom.xml, and
the necessary Spring configuration setting up the library with its
dependencies.

View File

@@ -7,14 +7,13 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:annotation-config/>
<context:annotation-config/>
<bean class="org.springframework.data.graph.neo4j.config.Neo4jConfiguration"/>
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
destroy-method="shutdown" scope="singleton">
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase" destroy-method="shutdown">
<constructor-arg index="0" value="target/config-test"/>
</bean>

View File

@@ -9,8 +9,6 @@
http://www.springframework.org/schema/data/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
">
<!--context:component-scan base-package="org.springframework.data.graph.neo4j"/-->
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test"/>
<bean id="config" class="org.springframework.data.graph.neo4j.config.DataGraphNamespaceHandlerTest$Config"/>

View File

@@ -13,7 +13,7 @@
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
destroy-method="shutdown" scope="singleton">
<constructor-arg index="0" value="target/config-test" />
<constructor-arg value="target/config-test" />
</bean>
<datagraph:config graphDatabaseService="graphDatabaseService"/>

View File

@@ -61,7 +61,7 @@
-->
<context:component-scan base-package="org.springframework.data.graph.neo4j">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.data.graph.neo4j.config.AbstractNeo4jConfiguration" type="regex"/>
<context:exclude-filter expression="org.springframework.data.graph.neo4j.config.Neo4jConfiguration" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

View File

@@ -111,29 +111,28 @@
<section>
<title>Configuring cross-store persistence</title>
<para>
Configuring cross-store persistence is done similarly to the default DATAGRAPH operations. The concise Spring Java Config configuration class
already contains a method <code>isUsingCrossStorePersistence</code>that must be implemented by a concrete configuration which controls
the cross-store mode of DATAGRAPH.
Configuring cross-store persistence is done similarly to the default DATAGRAPH operations. As soon as you refer
to an <code>entityManagerFactory</code> in the xml-namespace it is set up for cross-store persistence.
</para>
<programlisting lang="JAVA" ><![CDATA[
public class MyRestaurantConfig extends AbstractNeo4jConfiguration {
@Override
public boolean isUsingCrossStorePersistence() {
return true;
}
@Bean(destroyMethod = "shutDown")
public GraphDatabaseService graphDatabaseService() {
return new EmbeddedGraphDatabase("target/myrestaurant-social");
}
} ]]></programlisting>
<programlisting lang="XML" ><![CDATA[
<beans>
...
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="com.springone.myrestaurants.config.MyRestaurantConfig"/>
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
...
<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:datagraph="http://www.springframework.org/schema/data/graph"
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/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
">
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test" entityManagerFactory="entityManagerFactory"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
</bean>
</beans>
]]></programlisting>

View File

@@ -208,8 +208,8 @@ Iterable<Person> davesFriends = finder.findAllByTraversal(dave,
Neo4j is a transactional datastore which only allows modifications within transaction boundaries and fullfills the ACID properties.
Reading from the store is also possible outside of transactions. Neo4j also provides a Spring compliant transaction manager that
allows it to participate in Spring managed transactions (also with @Transactional). This transaction manager is already configured
in the Spring Java config, class AbstractNeo4jConfiguration.
</para>
in the Spring Java config class <code>Neo4jConfiguration</code>.
</para>
<para>
DATAGRAPH is designed to work within transaction boundaries. So entity creation and modification should happen within transactional methods.
Due to the usage of POJO entities it is common to create and populate them also outside of a transaction (e.g. in the web layer). That's why

View File

@@ -129,31 +129,76 @@
<para>an appropriate NodeTypeStrategy</para>
</listitem>
</itemizedlist>
That's why DATAGRAPH provides a Spring Java Config class (annotated with @Config) <code>AbstractNeo4jConfiguration</code> that takes care of all that. The only
thing that must be provided in the custom Spring config is the <code>GraphDatabaseService</code> configured with a datastore directory. This can be achieved
by extending that class and implementing the <code>graphDatabaseService</code> method.
<programlisting lang="JAVA"><![CDATA[
public class MyConfig extends AbstractNeo4jConfiguration {
@Override
public boolean isUsingCrossStorePersistence() {
return false;
}
@Bean(destroyMethod = "shutDown")
public GraphDatabaseService graphDatabaseService() {
return new EmbeddedGraphDatabase("target/neo4j-db");
}
} ]]></programlisting>
<programlisting lang="XML"><![CDATA[
<beans>
...
</para>
<section>
<title>XML-Namespace</title>
<para>
To simplify the configuration we provide a xml namespace <code>datagraph</code> that allows configuration of any
DATAGRAPH project with a single line of xml code. There are three possible parameters. You can use <code>storeDirectory</code>
or the reference to <code>graphDatabaseService</code> alternatively. For cross-store configuration just refer
to an <code>entityManagerFactory</code>.
</para>
<programlisting lang="XML"><![CDATA[
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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:datagraph="http://www.springframework.org/schema/data/graph"
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/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
">
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.data.graph.examples.config.MyConfig"/>
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
...
</beans>
]]></programlisting>
</para>
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test"/>
</beans>
]]></programlisting>
<programlisting lang="XML"><![CDATA[
<context:annotation-config/>
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase" destroy-method="shutdown">
<constructor-arg index="0" value="target/config-test" />
</bean>
<datagraph:config graphDatabaseService="graphDatabaseService"/>
]]></programlisting>
<programlisting lang="XML"><![CDATA[
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test" entityManagerFactory="entityManagerFactory"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
</bean>
]]></programlisting>
</section>
<section>
<title>Java based Configuration</title>
<para>
That's why DATAGRAPH provides a Spring Java Config class (annotated with @Config) <code>Neo4jConfiguration</code> that takes care of all that. The only
thing that must be provided in the custom Spring config is the <code>GraphDatabaseService</code> configured with a datastore directory.
<programlisting lang="XML"><![CDATA[
<beans>
...
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.data.graph.neo4j.config.Neo4jConfiguration"/>
<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
destroy-method="shutdown" scope="singleton">
<constructor-arg index="0" value="target/config-test"/>
</bean>
...
</beans>
]]></programlisting>
</para>
</section>
</section>
</chapter>