diff --git a/spring-data-neo4j-rest/src/test/java/org/neo4j/rest/graphdb/SpringPluginInitializerTest.java b/spring-data-neo4j-rest/src/test/java/org/neo4j/rest/graphdb/SpringPluginInitializerTest.java index b652ab5a6..841fe1520 100644 --- a/spring-data-neo4j-rest/src/test/java/org/neo4j/rest/graphdb/SpringPluginInitializerTest.java +++ b/spring-data-neo4j-rest/src/test/java/org/neo4j/rest/graphdb/SpringPluginInitializerTest.java @@ -42,13 +42,21 @@ public class SpringPluginInitializerTest extends SpringPluginInitializer impleme private static int touched = 0; private static final String HOSTNAME = "localhost"; private static final int PORT = 7473; -// -// @Path( "/testInterface" ) -// @POST -// @Produces( MediaType.APPLICATION_JSON ) -// public void runThis( @Context TestInterface test ) { -// test.thisIsARecording(); -// } + + @Path( "/testInterface" ) + @POST + @Produces( MediaType.APPLICATION_JSON ) + public void runThis( @Context TestInterface test ) { + test.thisIsARecording(); + } + + @Test + public void shouldInjectInterface() throws Exception { + ClientResponse response = sendRequest( "testInterface" ); + + Assert.assertEquals( 204, response.getStatus() ); + Assert.assertEquals( 1, touched ); + } @Path( "/testConcrete" ) @POST @@ -92,15 +100,6 @@ public class SpringPluginInitializerTest extends SpringPluginInitializer impleme Assert.assertEquals( 1, touched ); } - - @Test - public void shouldInjectInterface() throws Exception { - ClientResponse response = sendRequest( "testInterface" ); - - Assert.assertEquals( 204, response.getStatus() ); - Assert.assertEquals( 1, touched ); - } - private ClientResponse sendRequest( String method ) { return Client.create(). resource( "http://localhost:7473/test/" + method ). diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/server/SpringPluginInitializer.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/server/SpringPluginInitializer.java index 2269deb87..4f85136b3 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/server/SpringPluginInitializer.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/server/SpringPluginInitializer.java @@ -17,14 +17,14 @@ package org.springframework.data.graph.neo4j.server; import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.beanutils.BeanFactory; import org.neo4j.graphdb.GraphDatabaseService; -import org.neo4j.server.plugins.PluginLifecycle; 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 @@ -44,7 +44,7 @@ public abstract class SpringPluginInitializer implements PluginLifecycle { private String[] exposedBeans; protected ProvidedClassPathXmlApplicationContext ctx; - public SpringPluginInitializer(String[] contextLocations, String... exposedBeans) { + public SpringPluginInitializer( String[] contextLocations, String... exposedBeans ) { this.contextLocations = contextLocations; this.exposedBeans = exposedBeans; } @@ -52,16 +52,31 @@ public abstract class SpringPluginInitializer implements PluginLifecycle { /** * Binds the provided graph database to the spring contexts so that spring beans that consume a * graph database can be populated.
+ * * @param graphDatabaseService of the Neo4j server - * @param config of the Neo4j Server + * @param config of the Neo4j Server * @return Exposes the requested Spring beans as @{see Injectable}s */ @Override - public Collection> start(GraphDatabaseService graphDatabaseService, Configuration config) { - ctx = new ProvidedClassPathXmlApplicationContext(graphDatabaseService, contextLocations); - Collection> result = new ArrayList>(exposedBeans.length); - for (final String exposedBean : exposedBeans) { - result.add(new SpringBeanInjectable(SpringPluginInitializer.this.ctx, exposedBean)); + public Collection> start( GraphDatabaseService graphDatabaseService, Configuration config ) { + ctx = new ProvidedClassPathXmlApplicationContext( graphDatabaseService, contextLocations ); + Collection> result = new ArrayList>( 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> getInjectablesForInterfaces( ProvidedClassPathXmlApplicationContext appCtx, + String exposedBean, + Class concreteType ) { + + ArrayList> result = new ArrayList>(); + for ( Class iface : concreteType.getInterfaces() ) { + result.add( new SpringBeanInjectable( appCtx, exposedBean, iface ) ); } return result; } @@ -70,31 +85,34 @@ public abstract class SpringPluginInitializer implements PluginLifecycle { * closes the spring context */ public void stop() { - if (ctx!=null) { + if ( ctx != null ) { ctx.close(); } } /** * provides access to the Spring bean, proxying the @{see Injectable} + * * @param optional type of the bean */ private static class SpringBeanInjectable implements Injectable { private final String exposedBean; protected ApplicationContext ctx; + private final Class clazz; - public SpringBeanInjectable(final ApplicationContext ctx, String exposedBean) { + public SpringBeanInjectable( final ApplicationContext ctx, String exposedBean, Class clazz ) { this.exposedBean = exposedBean; this.ctx = ctx; + this.clazz = clazz; } public T getValue() { - return (T) ctx.getBean(exposedBean); + return (T)ctx.getBean( exposedBean ); } public Class getType() { - return (Class) ctx.getType(exposedBean); + return clazz; } } }