Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 是否可以在不扩展GraphRespository的自定义接口上使用@Autowired?_Spring_Spring Data_Spring Data Neo4j - Fatal编程技术网

Spring 是否可以在不扩展GraphRespository的自定义接口上使用@Autowired?

Spring 是否可以在不扩展GraphRespository的自定义接口上使用@Autowired?,spring,spring-data,spring-data-neo4j,Spring,Spring Data,Spring Data Neo4j,我的工作是使用Spring和Spring数据Neo4j 这一问题是对“”的后续行动,该问题导致了两项相辅相成的行动: 修改,以便使用接口而不是Spring-Data-Neo4J注释类来定义我的域(实现已移动到子项目,因此所有Neo4J特定的代码都位于数据访问层项目中,这正是我想要的) 创建一个新DAO,其中包含对Spring数据Neo4j的零引用 我的问题是行动2 目前,我有一些特定于SDN的存储库,例如: 包eu.ueb.acem.dao.bleu.neo4j; 导入eu.ueb.acem.d

我的工作是使用Spring和Spring数据Neo4j

这一问题是对“”的后续行动,该问题导致了两项相辅相成的行动:

  • 修改,以便使用接口而不是Spring-Data-Neo4J注释类来定义我的域(实现已移动到子项目,因此所有Neo4J特定的代码都位于数据访问层项目中,这正是我想要的)
  • 创建一个新DAO,其中包含对Spring数据Neo4j的零引用
  • 我的问题是行动2

    目前,我有一些特定于SDN的存储库,例如:

    包eu.ueb.acem.dao.bleu.neo4j;
    导入eu.ueb.acem.domain.beans.bleu.neo4j.BesoinNode;
    导入org.springframework.data.neo4j.repository.graphRespository;
    导入org.springframework.data.neo4j.repository.RelationshipOperationsRepository;
    公共接口BesoinRepository扩展了GraphRespository、RelationshipOperationsRepository{
    }
    
    如果我创建一个BesoinDAO接口来隐藏这个BesoinRepository接口,我想它应该是这样的:

    包eu.ueb.acem.dao.bleu;
    导入eu.ueb.acem.domain.beans.bleu.Besoin;
    公共接口BesoinDAO{
    public Besoin[]retrieveAll()抛出BesoinDAOException;
    公共Besoin检索(字符串名)抛出BesoinDAOException;
    公共无效插入(Besoin Besoin)引发BESOINDAO异常;
    公共void更新(Besoin-Besoin)抛出BesoinDAOException;
    公共void delete(Besoin Besoin)抛出BesoinDAOException;
    }
    
    然后:

    包eu.ueb.acem.dao.bleu.neo4j;
    进口eu.ueb.acem.dao.bleu.BesoinDAO;
    导入eu.ueb.acem.domain.beans.bleu.neo4j.BesoinNode;
    导入org.springframework.data.neo4j.repository.graphRespository;
    导入org.springframework.data.neo4j.repository.RelationshipOperationsRepository;
    公共接口BesoinRepository扩展了BesoinDAO、GraphRespository、RelationshipOperationsRepository{
    }
    
    不同之处在于,BesoinRepository现在扩展了BesoinDAO

    问题就在这里

    我已经编写了一个测试用例,它在BesoinRepository上使用@Autowired,并且运行良好。但是,如果我试图通过BesoinDAO获得Besoin,如下所示,它不再起作用:

    @Autowired
    私人贝松岛贝松岛;
    @试验
    公共最终无效t0TestCreateBesoin(){
    Besoin besoin1=新的BesoinNode(“t0TestCreateBesoin的besoin1”);
    Besoin besoin2=null;
    试一试{
    besoinDao.insert(besoin1);
    besoin2=besoinDao.retrieve(“用于t0TestCreateBesoin的besoin2”);
    }捕获(Besoindaoe){
    e、 printStackTrace();
    }
    assertNotNull(besoin2);
    }
    
    例外情况是:

    Tests in error: 
    t0TestCreateBesoin(eu.ueb.acem.repository.BesoinRepositoryTest): Error creating bean with name 'eu.ueb.acem.repository.BesoinRepositoryTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eu.ueb.acem.dao.bleu.BesoinDAO eu.ueb.acem.repository.BesoinRepositoryTest.besoinDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'besoinRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property update found for type eu.ueb.acem.domain.beans.bleu.neo4j.BesoinNode
    
    问题:在不扩展SDN的GraphRespository接口的接口上使用@Autowired是否可能?如果是,怎么做


    谢谢

    接口可以自动连接,在Spring Data Neo4j配置文件中声明实现的具体类,即:

    <bean id="besoinDao" class="eu.ueb.acem.dao.bleu.neo4j.BesoinRepository"/>
    
    
    
    问题在于泛型和我们有类型擦除的事实,这基本上使您的
    更新(Besoin)
    覆盖泛型。尝试
    更新(谢谢@M.Deinum的回答。你能给我确切的行来替换我的
    公共无效更新(Besoin-Besoin)吗抛出BesoinDAOException;
    声明?我不理解Hi!问题是BesoinRepository是一个接口,而不是一个类。因此我在创建名为“besoinDao”的bean时出错,该bean在类路径资源[test context.xml]中定义:bean实例化失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[eu.ueb.acem.dao.bleu.neo4j.BesoinRepository]:指定的类是一个接口
    。拥有两个接口有任何架构优势吗(即
    BesoinRepository扩展了graphrespository
    BesoinDAO
    )?我错过了它,您可以使用组合而不是继承,让BesoinRepository(或者更好的是BesoinDAO)作为一个类而不是一个接口,它可以实现DAO接口并具有对存储库的内部引用。感谢您将BesoinRepository嵌入其中的想法,它可以很好地工作并完全隐藏Neo4J。但是,我不能选择这一点作为可接受的答案,因为这与配置文件完全无关。:)