Changed so the SpringPluginInitializer takes which type to register as an injectable

This commit is contained in:
Andres Taylor
2011-04-05 10:26:41 +02:00
parent 911493370e
commit b0114866e4
3 changed files with 30 additions and 47 deletions

View File

@@ -23,6 +23,7 @@ import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.helpers.Pair;
import org.springframework.data.graph.neo4j.server.SpringPluginInitializer;
import javax.ws.rs.POST;
@@ -36,7 +37,7 @@ public class SpringPluginInitializerTest extends SpringPluginInitializer impleme
private LocalTestServer neoServer;
public SpringPluginInitializerTest() {
super( new String[]{"ServerTest-context.xml"}, "testObject" );
super( new String[]{"ServerTest-context.xml"}, Pair.of("testObject", TestInterface.class) );
}
private static int touched = 0;
@@ -50,21 +51,6 @@ public class SpringPluginInitializerTest extends SpringPluginInitializer impleme
test.thisIsARecording();
}
@Test
public void shouldInjectInterface() throws Exception {
ClientResponse response = sendRequest( "testInterface" );
Assert.assertEquals( 204, response.getStatus() );
Assert.assertEquals( 1, touched );
}
@Path( "/testConcrete" )
@POST
@Produces( MediaType.APPLICATION_JSON )
public void runThis( @Context SpringPluginInitializerTest test ) {
test.thisIsARecording();
}
@Path( "/testNoContext" )
@POST
@Produces( MediaType.APPLICATION_JSON )
@@ -84,6 +70,14 @@ public class SpringPluginInitializerTest extends SpringPluginInitializer impleme
neoServer.stop();
}
@Test
public void shouldInjectInterface() throws Exception {
ClientResponse response = sendRequest( "testInterface" );
Assert.assertEquals( 204, response.getStatus() );
Assert.assertEquals( 1, touched );
}
@Test
public void shouldWorkWithThirdPartyJaxrs() throws Exception {
ClientResponse response = sendRequest( "testNoContext" );
@@ -91,15 +85,6 @@ public class SpringPluginInitializerTest extends SpringPluginInitializer impleme
Assert.assertEquals( 204, response.getStatus() );
}
@Test
public void shouldInjectConcreteClass() throws Exception {
ClientResponse response = sendRequest( "testConcrete" );
Assert.assertEquals( 204, response.getStatus() );
Assert.assertEquals( 1, touched );
}
private ClientResponse sendRequest( String method ) {
return Client.create().
resource( "http://localhost:7473/test/" + method ).

View File

@@ -18,13 +18,13 @@ package org.springframework.data.graph.neo4j.server;
import org.apache.commons.configuration.Configuration;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.helpers.Pair;
import org.neo4j.server.plugins.Injectable;
import org.neo4j.server.plugins.PluginLifecycle;
import org.springframework.context.ApplicationContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Initializer to run Spring Data Graph based Server Plugins in a Neo4j REST-server. It takes the list of
@@ -39,12 +39,12 @@ import java.util.List;
* }
* </pre>
*/
public abstract class SpringPluginInitializer implements PluginLifecycle {
public abstract class SpringPluginInitializer<T extends Object> implements PluginLifecycle {
private String[] contextLocations;
private String[] exposedBeans;
private Pair<String, Class<T>>[] exposedBeans;
protected ProvidedClassPathXmlApplicationContext ctx;
public SpringPluginInitializer( String[] contextLocations, String... exposedBeans ) {
public SpringPluginInitializer( String[] contextLocations, Pair<String, Class<T>>... exposedBeans) {
this.contextLocations = contextLocations;
this.exposedBeans = exposedBeans;
}
@@ -62,21 +62,9 @@ public abstract class SpringPluginInitializer implements PluginLifecycle {
ctx = new ProvidedClassPathXmlApplicationContext( graphDatabaseService, contextLocations );
Collection<Injectable<?>> result = new ArrayList<Injectable<?>>( exposedBeans.length );
ProvidedClassPathXmlApplicationContext appCtx = SpringPluginInitializer.this.ctx;
for ( final String exposedBean : exposedBeans ) {
Class<?> concreteType = ctx.getType( exposedBean );
result.add( new SpringBeanInjectable( appCtx, exposedBean, concreteType ) );
result.addAll( getInjectablesForInterfaces( appCtx, exposedBean, concreteType ) );
}
return result;
}
private List<Injectable<?>> getInjectablesForInterfaces( ProvidedClassPathXmlApplicationContext appCtx,
String exposedBean,
Class<?> concreteType ) {
ArrayList<Injectable<?>> result = new ArrayList<Injectable<?>>();
for ( Class<?> iface : concreteType.getInterfaces() ) {
result.add( new SpringBeanInjectable( appCtx, exposedBean, iface ) );
for ( final Pair<String, Class<T>> exposedBean : exposedBeans ) {
// Class<?> concreteType = ctx.getType( exposedBean );
result.add( new SpringBeanInjectable( appCtx, exposedBean.first(), exposedBean.other() ) );
}
return result;
}

View File

@@ -39,16 +39,26 @@ org.neo4j.server.thirdparty_jaxrs_classes=com.example.mypackage=/my-context
</para>
<para>
Running Spring Data Graph on the server is easy. You need to tell the server where to find the Spring Context
file, and which beans from it to expose:
file, and which beans from it to expose, using what type:
<programlisting language="java"><![CDATA[
public class HelloWorldInitializer extends SpringPluginInitializer {
public HelloWorldInitializer() {
super(new String[]{"spring/helloWorldServer-Context.xml"},
"worldRepository",
"graphRepositoryFactory");
Pair.of("worldRepository", WorldRepository.class),
Pair.of("graphRepositoryFactory", GraphRepositoryFactory.class));
}
}
]]></programlisting>
Now, your resources can be annotated with the beans they need, like this:
<programlisting language="java"><![CDATA[
@Path( "/path" )
@POST
@Produces( MediaType.APPLICATION_JSON )
public void foo( @Context WorldRepository repo ) {
...
}
]]></programlisting>
The <code>SpringPluginInitializer</code> merges the graph database service
with the spring configuration and registers the named beans as jersey Injectables.
It is still necessary to list the initializer fully qualified class name in a