java - 非托管服务器插件中 Spring Data Neo4j 中的 Jersey/Spring 集成

标签 java spring neo4j cypher spring-data-neo4j

在尝试将 spring 和 jersey 集成到我的 spring-data-neo4j 非托管服务器插件中时,我有点迷失了。我创建了带有 Neo4j 注释的 POJO 模型,以便持久保存在商店中。最重要的是,我创建了 spring-data-neo4j 存储库用于数据操作。我创建了一个 springContext.xml 文件并将其放置在 resources 文件夹下。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

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

    <neo4j:repositories base-package="com.mycompany.poc.graph.repositories" /> 
    <context:component-scan base-package="com.mycompany.poc.rest" /> 
</beans>

此外,我还设置了 SpringPluginInitializer 并初始化了 springContext.xml。

import com.mycompany.poc.graph.repositories.NodeObjectGraphRepository;
public class SpringExtensionInitializer extends SpringPluginInitializer {

    @SuppressWarnings("unchecked")
    public SpringExtensionInitializer() {
        super(new String[] {
            "META-INF/spring/springContext.xml"
        }, expose("template", Neo4jTemplate.class), 
           expose("nodeObjectGraphRepository", NodeObjectGraphRepository.class),
           expose("pathController", PathController.class));        
    }

}

我已经通过 PathController 公开了 Jersey Rest 端点的业务服务。

package com.mycompany.poc.rest;
import com.mycompany.poc.graph.repositories.NodeObjectGraphRepository;

@Path("/path")
@Component
public class PathController {

    @Autowired NodeObjectGraphRepository nodeObjectGraphRepository;

    @GET
    @Path("/paths")
    @Produces(MediaType.APPLICATION_JSON)
    public Response paths(@QueryParam("startNode") Long startNodeId, @QueryParam("endNode") Long endNodeId) {
        Response response = null;
        if( startNodeId != null && endNodeId != null){
            try{
                EndResult endResult = nodeObjectGraphRepository.getPathsBetweenNodes(startNodeId, endNodeId);
                response = Response.ok().entity(endResult).build();

            }catch(Exception e){
                response = Response.serverError().entity(ExceptionUtils.getFullStackTrace(e)).build();
            }
        }else{
            response = Response.serverError().entity("Invalid Inputs").build();
        }        
        return response;
    }
}

我用 Spring 构造型“Component”注释了 PathController 类,并通过 Spring Autowiring 注入(inject)了“NodeObjectGraphRepository”对象。但是,我得到一个空的 nodeObjectGraphRepository 对象,因此无法使用存储库。在 springContext.xml 中,我设置了对 Spring 数据 Neo4j 存储库和 Jersey Controller 的扫描,并用 Spring 构造型“Component”进行注释。我无法弄清楚如何注入(inject) spring 依赖项。非常感谢任何帮助。

最佳答案

我解决了。感谢牛头人米尔斯。我关注了他的advice 。必须使用@InjectParam而不是@Autowired。我能够通过使用 @InjectParam 而不是 @Autowired 注释来注入(inject) spring bean。但是,我无法注入(inject) Spring Data Neo4j 存储库。当我启动 Neo4j 社区服务器时,我在控制台日志中收到错误。

INFO: Root resource classes found:
  class com.mycompany.poc.rest.PathController
May 03, 2014 6:36:55 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
May 03, 2014 6:36:55 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.9 09/02/2011 11:17 AM'
May 03, 2014 6:36:56 AM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  **SEVERE: The class com.mycompany.poc.graph.repositories.NodeObjectGraphRepository is an interface and cannot be instantiated.**
06:36:56.397 [AWT-EventQueue-0] WARN  /extensions - unavailable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException: null
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) ~[neo4j-desktop-2.0.2.jar:2.0.2]
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136) ~[neo4j-desktop-2.0.2.jar:2.0.2]
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199) ~[neo4j-desktop-2.0.2.jar:2.0.2]

关于java - 非托管服务器插件中 Spring Data Neo4j 中的 Jersey/Spring 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23422082/

相关文章:

Java继承——类型子类

java - 为什么这总是在主线程上运行?

spring - getcurrentsession 中的空指针异常

java - 如何在 Neo4J Java 中搜索处于一种关系而不是另一种关系的 endNode

nosql - 什么是已知的最大Neo4j集群?

neo4j - 为 OrientDB 重写 neo4j 查询

JavaFX Imageview 图像转换

java - Android - 无法以编程方式删除 Wifi 网络 - WifiManager 类型中的方法 removeNetwork(int) 不适用于参数(字符串)

java - 在 spring 中加载属性文件

java - Guice - Guice 中的 Spring Autowired 相当于什么